听说你Go语言学的不错,来来来,我这儿有几道小题

闲言少叙,直奔主题。


听说你Go语言学的不错,来来来,我这儿有几道小题


第1题

<code>package main

import (
"fmt"
)

func hello() []string {
return nil
}

func main() {
h := hello
if h == nil {
fmt.Println("nil")
} else {
fmt.Println("not nil")
}
}

/<code>

选项

<code>nil  
not nil
compilation error/<code>

答案

<code>not nil/<code>

在主函数中,把 hello 函数赋值给了变量 h。在条件判断中,h != nil,所以会打印,not nil。


第2题

<code>package main

import (
"fmt"
"strconv"
)

func main() {
i := 2
s := "1000"
if len(s) > 1 {
i, _ := strconv.Atoi(s)
i = i + 5
}
fmt.Println(i)
}

/<code>

选项

<code>2  
1005
compilation error/<code>

答案

<code>2/<code>

主要问题点集中在第 12 行, i, _ := strconv.Atoi(s) 创建了一个新变量i ,作用域限制在 if 语句内。 但是在第 15 行打印的时候,变量 i 则是全局变量的值。


第3题

<code>package main

import (
"fmt"
)

func hello(num ...int) {
num[0] = 18
}

func main() {
i := []int{5, 6, 7}
hello(i...)
fmt.Println(i[0])
}

/<code>

选项

<code>18  
5
Compilation error/<code>

答案

<code>18/<code>

这一题考查的是可变参数,如果把 slice 传递给可变参数,其作用会施加到调用者。

第 4 题

<code>package main

import (
"fmt"
)

func main() {
a := [2]int{5, 6}

b := [2]int{5, 6}
if a == b {
fmt.Println("equal")
} else {
fmt.Println("not equal")
}
}

/<code>

选项

<code>compilation error  
equal
not equal/<code>

答案

<code>equal/<code>

数组是Go中的值类型,可以进行比较。如果对应的元素相等,则两个数组值相等。在我们的例子中,a和b是相等的,因此这个程序打印为相等。


第 5 题

<code>package main

import "fmt"

type rect struct {
len, wid int
}

func (r rect) area() {
fmt.Println(r.len * r.wid)
}


func main() {
r := &rect{len: 5, wid: 6}
r.area()
}

/<code>

选项

<code>compilation error  
30/<code>

答案

<code>30/<code>

这个程序将正确编译并打印30。

第 14 行,我们把 rect 的地址分配给了 r。你可能想知道,当我们在第 15 行没有使用(*r).area()时,为什么这个程序还能工作。由于area()有一个值接收器,所以Go足够智能,可以将 r.area() 解释为 (*r).area(),因此这个程序可以工作:。


第 6 题

<code>package main

import (
"fmt"
)

func main() {
a := [5]int{1, 2, 3, 4, 5}
t := a[3:4:4]

fmt.Println(t[0])
}

/<code>

选项

<code>3  
4
compilation error/<code>

答案

<code>4/<code>

表达式

<code>a[low : high : max]/<code>

构造与简单的片表达式 a[low: high] 相同类型、相同长度和元素的片。

另外,它通过将结果片设置为 max - low 来控制它的容量。因此,第 9 行中的切片 t 有一个元素4,容量是1。

第 7 题

<code>package main

import (
"fmt"
)

type person struct {
name string
}

func main() {
var m map[person]int

p := person{"mike"}
fmt.Println(m[p])
}

/<code>

选项

<code>compilation error  
0
1/<code>

答案

<code>0/<code>

当我们尝试打印在映射中不存在的元素时,将打印该元素的零值。在我们的例子中,m是 map[person]int 类型的映射。因为p在映射中不存在,int 的初始值 0 被打印出来。

第 8 题

<code>package main

import (
"fmt"
)

func main() {
i := 65
fmt.Println(string(i))
}

/<code>

选项

<code>A  
65
compilation error/<code>

答案

<code>A/<code>

字母 A 的 unicode值是 65。所以第 9 行,i 被强制格式化为string类型是,就打印出字母 A 了。

第 9 题

<code>package main

import (
"fmt"
)

func main() {
a := 5
b := 8.1
fmt.Println(a + b)
}

/<code>

选项

<code>13.1  
13
compilation error/<code>

答案

<code>compilation error/<code>

这个错误很简单,也很明显。两个变量数据类型不相同,直接运算,编译不通过。

第 10 题

<code>package main

import (
"fmt"
)

func main() {
var i interface{}
if i == nil {
fmt.Println("nil")
return
}
fmt.Println("not nil")
}

/<code>

选项

<code>nil  
not nil
compilation error/<code>

答案

<code>nil/<code>

空接口的基础值和具体类型都为nil。因此 i 为 nil。

第 11 题

<code>package main

import (
"fmt"
)

func hello(i int) {
fmt.Println(i)
}
func main() {
i := 5
defer hello(i)
i = i + 10
}

/<code>

选项

<code>5  
15/<code>

答案

<code>5/<code>

延迟函数的参数是在执行 defer 语句时求值,而不是在实际函数调用完成时求值。

因此,当遇到第 12 行中的 defer 语句时, i的值是5。这个程序会输出5。


第 12 题

<code>package main

import (
"fmt"
)

func main() {
fmt.Printf("%%")
}


/<code>

选项

<code>0.0  
compilation error
%/<code>

答案

<code>%/<code>

字符串格式化的知识。想打印 % 字面量,就需要用 % 进行转移。

第 13 题

<code>package main

import (
"fmt"
)

func main() {
s := make(map[string]int)
delete(s, "h")
fmt.Println(s["h"])
}

/<code>

选项

<code>runtime panic  
0
compilation error/<code>

答案

<code>0/<code>

第 9 行中的 delete 函数不返回任何内容,如果指定的键不存在,则不执行任何操作。

因为键 h 不存在,所以 delete 函数不会执行任何操作。

第 10 行,我们正在试着打印 s["h"] 。由于映射 s 没有关键字 h ,它将返回默认的 int 值,因此输出0。

第 14 题

<code>package main

import (
"fmt"
)

func main() {
i := -5
j := +5
fmt.Printf("%+d %+d", i, j)
}

/<code>

选项

<code>-5 +5
+5 +5
0 0/<code>

答案

<code>-5 +5/<code> 

格式化字符串“%+d”总是返回一个数值。因此程序输出 -5 +5。


纸上得来终觉浅,绝知此事要躬行。

赶紧打开电脑。在你熟悉的编辑器下面,实践上面的代码吧。


我是 @程序员小助手,持续分享编程故事,欢迎关注。


分享到:


相關文章: