教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 golang -- 面试题

golang -- 面试题

发布时间:2022-02-09   编辑:jiaochengji.com
教程集为您提供golang -- 面试题等资源,欢迎您收藏本站,我们将为您提供最新的golang -- 面试题资源
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"/></svg><h1>代码输出问题</h1> <h2>1. defer 栈结构</h2> <pre><code>package main import ( "fmt" ) func main() { DeferCall() } func DeferCall() { defer func() {fmt.Println("A")}() defer func() {fmt.Println("B")}() defer func() {fmt.Println("C")}() panic("error") } </code></pre>

输出结果

解释:
defer注册是方法是保存在栈中,后入先出。
panic触发后,defer方法仍然执行。但panic后面的代码不执行。

<h2>
2. go启动goroutine时传入参数</h2> <pre><code>package main import ( "fmt" "runtime" "sync" ) func main() { runtime.GOMAXPROCS(1) wg := sync.WaitGroup{} wg.Add(20) for i := 0; i < 10; i { go func() { fmt.Println("A: ", i) wg.Done() }() // 这里不传入参数 } for i := 0; i < 10; i { go func(i int) { fmt.Println("B: ", i) wg.Done() }(i) // 这里传入i } wg.Wait() } </code></pre>

运行结果:一共10个10

解释:
传入i时,go func 会一直保存i这个变量的引用,当i在外层循环变成了10, go func 保存的i也都是10.

<h2>
3. 继承方法</h2> <pre><code>package main import ( "fmt" ) 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() t.ShowB() } </code></pre>

运行结果:

解释:
Teacher覆盖了ShowB方法,调用ShowA方法时,ShowA属于People,所以ShowA里面调用的仍然是People的ShowB方法.

<h2>
4. 切片</h2> <pre><code>package main import ( "fmt" ) func main() { s := make([]int, 5) s = append(s, 1, 2, 3) fmt.Println(s) } </code></pre>

运行结果:

解释:
make([]int, 5) 之后,这个list就变成[0, 0, 0, 0, 0] 了,默认0填充。

<h2>
5. 接口</h2> <pre><code>package main import ( "fmt" ) type People interface { Show() } type Student struct{} func (stu *Student) Show() { } func main() { var s *Student fmt.Println(s == nil) // true var p People = s fmt.Println(p == nil) // false } </code></pre>

解释:
在底层,interface作为两个成员来实现,一个类型和一个值, 只有当类型未设置, 值也没有设置的时候,interface才是nil.

#代码存在问题

<h2>
1. for循环</h2> <pre><code>package main import ( "fmt" ) type student struct { Name string Age int } func PaseStudent() { m := make(map[string]*student) stus := []student{ {Name: "zhou", Age: 24}, {Name: "li", Age: 23}, {Name: "wang", Age: 22}, } for _, stu := range stus { m[stu.Name] = &stu // 这里的会把stu这个变量的地址赋值给map fmt.Printf("%p \n", &stu) // for循环的变量, 不会每次都被新建 } // 显示m中的内容 for k, v := range m { fmt.Println(k, *v) } } func main() { PaseStudent() } </code></pre>

运行结果:

错误:
for 循环不会每次循环的时候都新建循环变量stu, 所以将stu变量的地址赋值给map后,循环结束后,map中所以值还是指向stu地址。

改正:

<pre><code> for _, stu := range stus { s := stu // 这里新建一个变量 m[stu.Name] = &s fmt.Printf("%p \n", &stu) } </code></pre> <h2>
2. type使用</h2> <pre><code>package main 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 } </code></pre>

运行结果:

解释:type只能用在interface上, 把func GetValue返回值从int改为interface {} 就可以了。

到此这篇关于“golang -- 面试题”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
golang基础教程
Golang面试题集锦
golang面试经之笔试2
golang面试经之笔试1
连nil切片和空切片一不一样都不清楚?那BAT面试官只好让你回去等通知了
U3D笔试题1:golang实现
go golang 笔试题 面试题 笔试 面试
Golang 高频面试题七问
golang面试官:for select时,如果通道已经关闭会怎么样?如果select中只有一个case呢?
Golang template 高级问题

[关闭]
~ ~