Go Golang pen question face test interview

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

Go Golang pen question face test interview

Found that Go's written and interview topics are still relatively small, so I intend to summarize recently. It's not difficult, but it's easy to step on the pit if you're not ready to hit it.

is a few simple written questions, but also may be interviewed directly to see the results.

1, when using different goroutine to operate the map, there will be a thread synchronization problem, the map into an int, the problem also exists. It's like that in go.

2, the following code outputs the value:

    func (){    jsonStr:=[]byte(`{"age":1}`)    varmap[string]interface{}    json.Unmarshal(jsonStr,&value)    age:=value["age"]    fmt.Println(reflect.TypeOf(age))    //float64}

3. Is there a problem with the code below, and where is the problem?

    import (    "sync"    "fmt")typestruct {    map[stringint    sync.Mutex}funcstringint)  {    u.Lock()    defer u.Unlock()    u.ages[name] = age}funcstring)int{    if age,ok:=u.ages[name];ok{        return age    }    return -1}

The problem is that ages does not expose packets outside the back, causing the caller to fail to initialize the ages. Then, when the add function is called, an error is encountered. Investigate the scope of the 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=append4)    for i:=range s2{        s2[i]+=10    }    fmt.Println(s2)}

The output is as follows: This is in the study of arrays and slices with S2 intercept the bottom of the S1 is 1, and then perform the operation.

[12 13] [22 23 14]

5, what's the bottom code output?

"Go
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 code below?

//Put in main inside    //Specifies that only one logical processor can be used to easily see the scheduling order. Runtime. Gomaxprocs (1) Wg:=sync. waitgroup{} WG. ADD ( -) fori:=0;i<Ten; i++ {Go func() {FMT. Println ("I", i) WG. Done ()} ()} fori:=0;i<Ten; i++ {Go func(Iint) {FMT. Println ("J", i) WG. Done ()} (i)} WG. Wait ()/*J 9I tenI tenI tenI tenI tenI tenI tenI tenI tenI tenJ 0J 1J 2J 3J 4J 5J 6J 7J 8     */

This note that the first for does not have a parameter, and the second parameter is passed. So the first for the goroutine to start with I is actually the main line thread I, the reason is 10, (there may be several <10), because I was called when I in the main line range has been added to 10. The second for the I is passed through the parameters, so it will print 0~9;
As to why the j,9 is printed first, I hope there is a great God to answer it.

Write so much for the time being, and go back and Add.

Reprint annotated source-name White

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

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.