This is a creation in
Article, where the information may have evolved or changed.
Sharing a few go interview questions is very simple, if you have some tips on how to use the development time to welcome comments.
Currently I write 11 (then update a topic), the future will not be updated regularly ...
1, write the following code output.
package mainimport ("fmt")func main() {defer_call()}func defer_call() {defer func() { fmt.Println("打印前") }()defer func() { fmt.Println("打印中") }()defer func() { fmt.Println("打印后") }()panic("触发异常")}
2. What's wrong with the following code, explain why
type Student struct {Name string Age int}func pase_student() {m := Make(Map[string]*Student)Stus := []Student{{Name: "Zhou", Age: -},{Name: "Li", Age: at},{Name: "Wang", Age: A},} for _, Stu := Range Stus {m[Stu.Name] = &Stu}}
3, the following code will output what, and explain why
func 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("i: ", i)wg.Done()}(i)}wg.Wait()}
4. What will the following code output?
type People struct{}func (p *People) ShowA() {fmt.Println("showA")p.ShowB()}func (p *People) ShowB() {fmt.Println("showB")}type Teacher struct {People}func (t *Teacher) ShowB() {fmt.Println("teacher showB")}func main() {t := Teacher{}t.ShowA()}
5. Will the following code trigger an exception? Please explain in detail
func main() {runtime.GOMAXPROCS(1)int_chan := make(chan int, 1)string_chan := make(chan string, 1)int_chan <- 1string_chan <- "hello"select {case value := <-int_chan:fmt.Println(value)case value := <-string_chan:panic(value)}}
6. What does the following code output?
func calc(index string, a, b int) int {ret := a + bfmt.Println(index, a, b, ret)return ret}func main() {a := 1b := 2defer calc("1", a, calc("10", a, b))a = 0defer calc("2", a, calc("20", a, b))b = 1}
7. Please write down the following input content
func main() {s := make([]int, 5)s = append(s, 1, 2, 3)fmt.Println(s)}
8. What is the problem with the following code?
type UserAges struct {ages map[string]intsync.Mutex}func (ua *UserAges) Add(name string, age int) {ua.Lock()defer ua.Unlock()ua.ages[name] = age}func (ua *UserAges) Get(name string) int {if age, ok := ua.ages[name]; ok {return age}return -1}
9. What's wrong with the following iterations?
func (set *threadSafeSet) Iter() <-chan interface{} {ch := make(chan interface{})go func() {set.RLock()for elem := range set.s {ch <- elem}close(ch)set.RUnlock()}()return ch}
10, the following code can compile the past? Why?
package mainimport ("fmt")type People interface {Speak(string) string}type Stduent struct{}func (stu *Stduent) Speak(think string) (talk string) {if think == "bitch" {talk = "You are a good boy"} else {talk = "hi"}return}func main() {var peo People = Stduent{}think := "bitch"fmt.Println(peo.Speak(think))}
11, the following code to print out what content, say why ...
package mainimport ("fmt")type People interface {Show()}type Student struct{}func (stu *Student) Show() {}func live() People {var stu *Studentreturn stu}func main() {if live() == nil {fmt.Println("AAAAAAA")} else {fmt.Println("BBBBBBB")}}