Common knowledge points of golang

Topic link: https://zhuanlan.zhihu.com/p/26972862

  1. The defer sequence problem, the last definition is executed first, and the panic will be passed up after the defer, output: after printing, during printing, before printing, trigger an exception
  2. For range iteration will use a temporary variable as a value copy, m saves the address of the temporary variable
  3. The goroutine execution will save the variable address of for, and the first for loop i is a copy of the value, so the output is all 10; the second i is passed in as a copy of the gofunc parameter value, so each gofunc execution is a copy of the i at that time, So output 0 to 9. As for which gofunc is scheduled first, it depends on the goroutine scheduling mechanism, which should be related to each go version.
  4. Inheritance logic in most languages, parent class references will not invoke methods overloaded by subclasses
  5. There may or may not be exceptions. select statement: If an IO operation occurs in a case (channel), it will respond, otherwise it will continue to block. Therefore, only one case in select can be executed, depending on golang's random selection; as long as there is one case in select that can be returned, it will be executed immediately. When multiple cases can return at the same time, any one of them is selected and executed in a pseudo-random manner. If none of the cases can return then the "default" block can be executed.
  6. The execution order of the defer function is executed first, and the parameter is a copy of the value, which is only related to the copy value at that time, so index:2 must be executed first in index:1, but when the defe function is defined, the parameters have been determined, Plus index:1 is defined before index:2, so index:10 is executed before index:20.
  7. make initialization will have default values, so there is an array [0,0,0,0,0] before append
  8. There will be competition in map reading and writing, and panic will appear: concurrent map read and map write, you can refer to the recent blog post: https://blog.csdn.net/qq_17612199/article/details/78958238
  9. The chan buffer problem, if you define make([]chan) to be unbuffered, and write when chan is not read, the write operation will be blocked, which means that the write operation will not return until the data is not processed; on the contrary, if you use If the buffer is removed, the write operation will not be blocked.
  10. It cannot be compiled. First of all, there is no inheritance relationship between Student and People, and you will get an error can't not use xxx as type xxx. Second, the overloaded method sets are not defined differently, so the definitions must be consistent. Finally, people do not implement the Speak method and use peo Calling the Speak method by reference will result in an error.
  11. interface{} is not equal to nil, the underlying structure will store variables, types, method sets and other information.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324770738&siteId=291194637