In Golang, _
(underscore) is a special identifier. A few days ago, I read the gin source code and saw an interesting usage. Although there are many summary blogs online, they are always a little lacking, so this article is available for easy access in the future.
Used in import
This usage is common when guiding packages, especially when using mysql in projects or using pprof for performance analysis, for example
import _ "net/http/pprof" import _ "/go-sql-driver/mysql"
This usage will call the packageinit()
Functions, which initialize the imported package, but do not use other functions in the package.
Used to return value
This usage is also a common usage. The function returns multiple values in Golang, and err is usually the last value of the return value. However, sometimes we don't care about a certain value in the function return value. If you receive this value but don't use it, the code compilation will report an error, so it needs to be ignored. for example
for _, val := range Slice {} _, err := func()
Used in variables
We all know that the interface of the Go language is non-invasive, unlike Java and C++. As long as a structure implements all functions defined by the interface, we say that this interface implements the interface. There is a special name to indicate this behavior, duck typing, i.e.When you see a bird walking like a duck, swimming like a duck, and screaming like a duck, then the bird can be called a duck。
type I interface { Sing() } type T struct { } func (t T) Sing() { } type T2 struct { } func (t *T2) Sing() { } // Compile throughvar _ I = T{} // Compile throughvar _ I = &T{} // Compilation failedvar _ I = T2{} // Compile throughvar _ I = &T2{}
Here the underscore is used to determine whether the structure has implemented an interface. If it is not implemented, the problem can be exposed during compilation. Without this judgment, the compiler will not report an error using an interface method that is not implemented in the structure in the later code.
You can see that only the third one failed in the above four judgments, and the error is as follows:
./:27:5: cannot use T2 literal (type T2) as type I in assignment:
T2 does not implement I (Sing method has pointer receiver)
Why is this? After careful reading the above code, I found thatT
Implemented Sing
method, *T2
ImplementedSing
method.
We all know that Go is passed by value.
That's forT2
To say, callSing
When copying a copy, then take the address. The structure of the original call cannot be found through this address, but receiver is a pointer, indicating that this call needs to change the caller's internal variable. It is obvious that T2
Type calls cannot complete to achieve this goal, so an error is required here. And &T2
CallSing
Method, then it is OK, so no error is reported.
And for T
For example, no matter whether there is a pointer call, there will be no error. In fact, Go will be automatically implemented. *T
ofSing
method.
Of course, these are my personal understanding. If it is not correct, please feel free to correct it.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.