SoFunction
Updated on 2025-03-05

Golang novices may have minor problems if they are not paying attention to

Go Introduction

Language Philosophy

C language is purely procedural, which is related to the historical background it produces. The Java language is a radical object-oriented advocate, and the typical manifestation is that it cannot tolerate the existence of isolated functions in the system. Go language did not deny any party, but used a critical and absorbing perspective to sort out all programming ideas and integrate the strengths of all schools. However, it is always vigilant about the complexity of characteristics, strives to maintain the simplicity of language characteristics, and strives to be small and precise.

Go language opposes overloading of functions and operators, while C++, Java and C# all allow functions or operators of the same name to appear as long as their parameter list is different.

Secondly, Go language supports the combination of classes, class member methods, and classes, but opposes inheritance and opposes virtual functions and virtual function overloading. To be precise, Go also provides inheritance, but uses combination grammar to provide

Recently, I wrote the Youdao Cloud notes recorded during my study and found some interesting points to share with you. If there is any error, please point it out.

1. Closure defer

Closure (anonymous function)

func test(){
 i, n := 1 ,2;
 defer func(a int){
  ("defer:", a , n); //n is referenced by closure }(i) //Copy the value of i i , n = i+1,n+2;
 (i , n);
}

Let's look at the results:

2 4
defer: 1 4

Why is this happening? It is because the closure copyes the original object pointer, and delayed reference occurs. We should pay attention to this issue when using closures, and similar phenomena will also occur in the for loop.

2. Map

I saw a problem on the forum some time ago

type Data struct{
 AABB [2]float64
}
var m map[string]Data = make(map[string]Data,1)
m["xxx"] = Data{}
m["xxx"].AABB[0]=1.0
m["xxx"].AABB[1]=2.0<br data-filtered="filtered">#The above codego build Can't get it,Error messagecannot assign to m["xxx"].AABB[0]

This is an answer given by a netizen

type Data struct{
 AABB [2]float64
} 
m := make(map[string]*Data,1)
m["xxxx"] = Data{}
m["xxxx"].AABB[0] = 1.0
m["xxxx"].AABB[1] = 2.0
#That's right to write,your m["xxxx"] Returns the value,Not a desirable variable

This netizen's answer can be compiled successfully, but it is not advisable. He has committed many problems that are prone to newbies.

Why? The map element attribute in Golang is designed to be read-only and is not expected to be modified, and what is retrieved from the map is also a temporary copy. And map is a hash structure. When the hash is expanded, the storage location of key values ​​will change. If we modify m["xxxx"].AABB[0] = 1.0 at this time, we don't know what the pointer will send. If you are interested, you can take a look at Go Hashmap memory layout and implementation

If we want to modify it, it's better

type Data struct{
 AABB [2]float64
}
m := make(map[string]*Data,1)
m["xxxx"] = &Data{}
d := m["xxxx"]
[0] = 1.0
[1] = 2.0
m["xxxx"] = d

3. nil

First look at a piece of code. Of course, this kind of scenario is not common, but it can help us understand nil better

func t(){
 var i *int = nil;
 var n interface{} = i;
 (n==nil); //false
}

Many friends may have questions about why nil is not equal. Let's first look at the structure of pointer and interface and the structure when pointer and interface are nil

uintptr
type interfaceStruct struct {
v *_value // Actual valuet *_type // Type information of actual value}
uintptr(0) == nil
type interfaceStruct struct {
v:uintptr(0)
t:uintptr(0)
} == nil

From this we can see that nil is actually the zero value of the pointer interface

At this time, we are explaining why it is easy to use Flash

func t(){
 var i *int = nil; // (*int)nil
 var n interface{} = i; // interace{}((*int)nil)
 (n==nil); // type interfaceStruct struct {
   //  v: uintptr(0),
   //  t: (*int)
   // }
}

The official documentation stipulates that the types that can be nil include slice , map , channel , function .

Some friends may ask why there is no error type, because error is just an interface method preset by the program. Similar problems will occur in err nil. There is an official document that also explains it.

type error interface { 
 Error() string 
}

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.