2020 Go language must see new interview questions

1. What happens when the following code is compiled and executed?

package main
var(
    size :=1024
    max_size = size*2
)
func main()  {
    println(size,max_size)
}
Copy the code

Variable short mode Restrictions: – Define variables that are explicitly initialized at the same time – cannot provide data types – can only be used inside functions

Results:

syntax error: unexpected :=
Copy the code

2. What’s wrong with the following function?

package main
const cl  = 100

var bl    = 123

func main()  {
    println(&bl,bl)
    println(&cl,cl)
}
Copy the code

C. constant D. constantconstantUnlike variables, which allocate memory at runtime, constants are usually expanded by the compiler as instruction data during preprocessing.

cannot take the address of cl
Copy the code

3. What happens when the following code is compiled and executed?

package main func main() { for i:=0; i<10 ; i++ { loop: println(i) } goto loop }Copy the code

Goto cannot jump to other functions or inner code

goto loop jumps into block starting at
Copy the code

4. What happens when the following code is compiled and executed?

package main import "fmt" func main() { type MyInt1 int type MyInt2 = int var i int =9 var i1 MyInt1 = i var i2 MyInt2 =  i fmt.Println(i1,i2) }Copy the code

Go 1.9 is a new featureType Alias Create a new type based on a type, called defintion; Create an alias based on a type and call it alias. MyInt1 is called defintion. Although the underlying type is int, it cannot be directly assigned, so it needs to be strongly converted. MyInt2 is called alias and can be assigned directly.

Results:

cannot use i (type int) as type MyInt1 in assignment
Copy the code

5. What happens when the following code is compiled and executed?

package main
import "fmt"

type User struct {
}
type MyUser1 User
type MyUser2 = User
func (i MyUser1) m1(){
    fmt.Println("MyUser1.m1")
}
func (i User) m2(){
    fmt.Println("User.m2")
}

func main() {
    var i1 MyUser1
    var i2 MyUser2
    i1.m1()
    i2.m2()
}
Copy the code

A.Go 1.9 new featuresThe Type Alias, because MyUser2 is exactly equivalent to User, has all of its methods, and one of them has a new method, as does the other. but

i1.m2()
Copy the code

Is not executable because MyUser1 does not define the method. Results:

MyUser1.m1
User.m2
Copy the code

6. What happens when the following code is compiled and executed?

package main

import "fmt"

type T1 struct {
}
func (t T1) m1(){
    fmt.Println("T1.m1")
}
type T2 = T1
type MyStruct struct {
    T1
    T2
}
func main() {
    my:=MyStruct{}
    my.m1()
}
Copy the code

The Go 1.9 new feature Type Alias ** does not compile properly.

ambiguous selector my.m1
Copy the code

Results are not limited to methods, but also fields; This is not limited to type alias, but also type defintion. This occurs whenever there are duplicate methods, fields, because you don’t know which one to select. To:

my.T1.m1()
my.T2.m1()
Copy the code

The definition of a type alias is essentially the same type, but with an alias that uses the source type as well as the alias type, preserving all the methods, fields, and so on.

What happens when the following code is compiled and executed?

package main import ( "errors" "fmt" ) var ErrDidNotWork = errors.New("did not work") func DoTheThing(reallyDoIt bool) (err error) { if reallyDoIt { result, err := tryTheThing() if err ! = nil || result ! = "it worked" { err = ErrDidNotWork } } return err } func tryTheThing() (string,error) { return "",ErrDidNotWork } func main() { fmt.Println(DoTheThing(true)) fmt.Println(DoTheThing(false)) }Copy the code

Because err variables in the if block mask err variables in the scope of the function, the result:

<nil>
<nil>
Copy the code

To:

func DoTheThing(reallyDoIt bool) (err error) { var result string if reallyDoIt { result, err = tryTheThing() if err ! = nil || result ! = "it worked" { err = ErrDidNotWork } } return err }Copy the code

What happens when the following code is compiled and executed?

package main func test() []func() { var funs []func() for i:=0; i<2 ; i++ { funs = append(funs, func() { println(&i,i) }) } return funs } func main(){ funs:=test() for _,f:=range funs{ f() } }Copy the code

Closure delay evaluation for loop reuse of local variable I, each anonymous function put into the application is a variable. Results:

0xc042046000 2
0xc042046000 2
Copy the code

If not, you can change it to:

func test() []func() { var funs []func() for i:=0; i<2 ; i++ { x:=i funs = append(funs, func() { println(&x,x) }) } return funs }Copy the code

What happens when the following code is compiled and executed?

package main

func test(x int) (func(),func())  {
    return func() {
        println(x)
        x+=10
    }, func() {
        println(x)
    }
}

func main()  {
    a,b:=test(100)
    a()
    b()
}
Copy the code

The closure references the same variable.

100, 110,Copy the code

10. What happens when the following code is compiled and executed?

package main import ( "fmt" ) func main() { defer func() { if err:=recover(); err! =nil{ fmt.Println(err) }else { fmt.Println("fatal") } }() defer func() { panic("defer panic") }() panic("panic") }Copy the code

1. Only the last panic can be caught by Revover and defer is executed in sequence after triggering a panic(“panic”). However, there is still a panic in the defer, so the previous panic(“panic”) is overwritten.

defer panic
Copy the code

※ Some articles from the network, if any infringement, please contact to delete; More articles and materials | click behind the text to the left left left 100 gpython self-study data package Ali cloud K8s practical manual guide] [ali cloud CDN row pit CDN ECS Hadoop large data of actual combat operations guide the conversation practice manual manual Knative cloud native application development guide OSS Operation and maintenance actual combat manual cloud native architecture white paper Zabbix enterprise distributed monitoring system source document 10G large factory interview questions