go golang pen written interview questions interview questions

Go find the written questions and interview questions are still relatively small, and thus it is intended to sum up recently. Although it is not difficult, but if there is no ready suddenly encountered was quite easy to step on the pit.

That is, a few simple written questions, the interview also possible to directly see the results lets say.
go face questions stronghold

1, when used to operate different goroutine map will be problematic thread synchronization, the map into int, this problem also exists. Go in there like this.
2, the following code is output:
    func (){
    jsonStr:=[]byte(`{"age":1}`)
    var value map[string]interface{}
    json.Unmarshal(jsonStr,&value)
    age:=value["age"]
    fmt.Println(reflect.TypeOf(age))
    //float64
}
3, below the code if there is a problem, where is the problem

    import (
    "sync"
    "fmt"
)
type UserAges struct {
    ages map[string] int
    sync.Mutex
}

func (u *UserAges)Add(name string,age int)  {
    u.Lock()
    defer u.Unlock()
    u.ages[name] = age
}

func (u *UserAges)Get(name string)int{
    if age,ok:=u.ages[name];ok{
        return age
    }
    return -1
}

The problem is that, ages package back is not exposed to the outside, leading to the caller could not be initialized ages. Then when you call the add function will error. Examine the scope of the problem function.

4. What is the output of the code below?
func TestArrayAndSlice(){
    s1:=[]int{1,2,3}
    s2:=s1[1:]
    for i:=range s2{
        s2[i]+=10
    }
    fmt.Println(s2)
    s2=append(s2, 4)
    for i:=range s2{
        s2[i]+=10
    }
    fmt.Println(s2)
}

The output: this is taken below with s1 and s2 array in the study and after the slices 1; then perform the operation.

[12 13]
[22 23 14]

5, below what the code output
func TestDoit(){
   doit:= func(arg int) interface{}{
       var result *struct{}=nil

       if (arg>0) {
           result = &struct{}{}
       }
       return result
   }
   //输出结果。
   //-1:result: <nil>    为空的匿名结构体
   //1://result: &{}     匿名结构体的地址
   if res:=doit(1);res!=nil{
       fmt.Println("result:",res)
   }

}
6. What is the output of the following code
//放在main里边
    //指定只能用一个逻辑处理器,方便看调度顺序。
    runtime.GOMAXPROCS(1)
    wg:=sync.WaitGroup{}
    wg.Add(20)
    for i:=0;i<10 ;i++  {
        go func() {
            fmt.Println("i",i)
            wg.Done()
        }()
    }
    for i:=0;i<10 ;i++  {
        go func(i int) {
            fmt.Println("j",i)
            wg.Done()
        }(i)
    }
    wg.Wait()

    /*
    j 9
    i 10
    i 10
    i 10
    i 10
    i 10
    i 10
    i 10
    i 10
    i 10
    i 10
    j 0
    j 1
    j 2
    j 3
    j 4
    j 5
    j 6
    j 7
    j 8
     */

This should be noted that there are no pass for the first parameter, the second parameter passed. So where to start first for goroutine i actually used in the main thread i; the reason is 10, (also possible in front of several <10); because of the time i call, i have been added in the main thread up to 10. And the second for where i is passed through parameters, it will print 0-9;
As for why will first print J, 9; want to have to answer the next big God.

Being the first write so, then go back and add.

Reproduced indicate the source - the name White

http://www.cnblogs.com/mingbai/p/go-golangCodingTest.html

go face questions stronghold

Guess you like

Origin www.cnblogs.com/miansheng/p/11293723.html