12. Is it possible to compile the pass? If so, what does it output?
Func Main () {i: = GetValue () switch I (type) {case int:println ("int.") Case STRING:PRINTLN ("string") Case interface{}: println ("interface") Default:println ("Unknown")}}func GetValue () int {return 1}
Analytical
Test Centers:type
Compilation failed because type can only be used in interface
13. What's wrong with the following function?
Func funcmui (x, y int) (sum int,error) {return X+y,nil}
Analytical
Test Center: function return value named when there are multiple return values for a function, the other must have a name as long as there is a specified name for the return value. If the return value has more than one return value must be enclosed in parentheses, if there is only one return value and there is a name, parentheses are required; The first return value of the function here has the sum name, the second is named, so the error.
14. Is it possible to compile the pass? If so, what does it output?
Package MainFunc Main () {println (DEFERFUNC1 (1)) println (DEFERFUNC2 (1)) println (DeferFunc3 (1))}func DeferFunc1 (i int) ( t int) {t = Idefer func () {T + = 3} () return t}func DeferFunc2 (i int) int {t: = Idefer func () {T + = 3} () return T}func Deferf UNC3 (i int) (t int) {defer func () {T + = i} () return 2}
Analytical
Test Center:defer and function return values It is important to be clear that defer needs to be executed before the function ends. The function return value name is initialized at the beginning of the function to the corresponding type of 0 value and scope for the entire function DeferFunc1 has function return value T scope for the entire function, before return defer will be executed, so T will be modified, return 4; The scope of T in the DEFERFUNC2 function is a function and returns 1; DEFERFUNC3 return 3
15. Is it possible to compile the pass? If so, what does it output?
Func main () {list: = new ([]int) List = append (list, 1) fmt. PRINTLN (List)}
Analytical
Test Centers:New list:=make ([]int,0)
16. Is it possible to compile the pass? If so, what does it output?
Package Mainimport "FMT" Func Main () {s1: = []int{1, 2, 3}s2: = []int{4, 5}s1 = append (S1, S2) fmt. Println (S1)}
Analytical
Test Center:Append Append slicing time don't miss ' ... '
17. Is it possible to compile the pass? If so, what does it output?
Func Main () {sn1: = struct {age intname string}{age:11, Name: ' QQ '}SN2: = struct {age intname string}{age:11, name: "Q Q "}if SN1 = = SN2 {fmt. Println ("SN1 = = SN2")}sm1: = struct {age Intm map[string]string}{age:11, m:map[string]string{"a": "1"}}sm2: = struct { Age Intm Map[string]string}{age:11, m:map[string]string{"a": "1"}}if sm1 = = sm2 {fmt. Println ("SM1 = = Sm2")}}
Analytical
Test Center : Structure comparison in structural comparison, only the same type of structure can be compared, the structure of the same is not only related to the number of attribute types, but also related to the order of attributes.
sn3:= struct {name string age Int}{age:11,name: "QQ"}
Sn3 and SN1 are not the same structure, can not be compared. It is also important to note that structs are the same, but there are types in struct properties that cannot be compared, such as Map,slice. If the structure property is comparable, then you can use "= =" to compare operations.
You can use reflect. Deepequal For comparison
If reflect. Deepequal (SN1, SM) {FMT. Println ("SN1 ==sm")}else {fmt. Println ("SN1!=sm")}
So the compilation does not pass: invalid OPERATION:SM1 = = SM2
18. Is it possible to compile the pass? If so, what does it output?
Func Foo (x interface{}) {if x = = Nil {fmt. PRINTLN ("Empty Interface") return}fmt. PRINTLN ("Non-empty interface")}func main () {var x *int = Nilfoo (x)}
Analytical
Test Center:interface Internal Structure
Non-empty interface
19. Is it possible to compile the pass? If so, what does it output?
Func GetValue (m map[int]string, id int) (string, bool) {If _, exist: = M[id]; exist {return "existence data", True}return Nil, false }func Main () {intmap:=map[int]string{1: "A", 2: "BB", 3: "CCC",}v,err:=getvalue (intmap,3) fmt. Println (V,err)}
Analytical
Test Center: function return value type nil can be used as a "null" for interface, function, pointer, map, slice, and channel. However, if not specifically specified, the Go language does not recognize the type, so it will error. Usually compile without error, but run time will be reported: cannot use nil as type string in return argument .
20. Is it possible to compile the pass? If so, what does it output?
Const (x = Iotayz = "ZZ" KP = Iota) func main () {FMT. Println (X,Y,Z,K,P)}
Analytical
Test centers:Iota
Results:
0 1 ZZ ZZ 4