Analysis of Golang interview (II.)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

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 Centers: function return value naming
When a function has more than one return value, 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 not 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 DeferFunc3(i int) (t int) {defer func() {t += i}()return 2}

Analytical

Test centers: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;d The scope of T in the EFERFUNC2 function is a function, and return 1;DEFERFUNC3 returns 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 Centers:Append
Don't miss out on the Append section ' ... '

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: "qq"}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 centers: Structural comparisons
In structural comparison, only structs of the same type can be compared, and the structure 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 "存在数据", 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 Centers: 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. Newspaper: 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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.