-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
--- | ||
title: Go 语言笔试面试题(代码输出) | ||
seo_title: 极客面试 | ||
date: 2020-10-10 23:10:10 | ||
description: Go 语言/golang 笔试题,面试题,基础语法与内部实现原理,包括不限于垃圾回收机制(GC)、面向对象、并发编程等。 | ||
tags: | ||
- Go | ||
nav: 面试 | ||
categories: | ||
- Go 语言面试题 | ||
image: post/qa-golang/go_questions.jpg | ||
github: https://github.com/geektutu/interview-questions | ||
--- | ||
|
||
 | ||
|
||
**[Go 语言笔试面试题汇总](https://geektutu.com/post/qa-golang.html),[Github](https://github.com/geektutu/interview-questions)** | ||
|
||
## 常量与变量 | ||
|
||
1. 下列代码的输出是: | ||
|
||
```go | ||
func main() { | ||
const ( | ||
a, b = "golang", 100 | ||
d, e | ||
f bool = true | ||
g | ||
) | ||
fmt.Println(d, e, g) | ||
} | ||
|
||
``` | ||
|
||
<details> | ||
<summary>答案</summary> | ||
<div> | ||
|
||
golang 100 true | ||
|
||
在同一个 const group 中,如果常量定义与前一行的定义一致,则可以省略类型和值。编译时,会按照前一行的定义自动补全。即等价于 | ||
|
||
```go | ||
func main() { | ||
const ( | ||
a, b = "golang", 100 | ||
d, e = "golang", 100 | ||
f bool = true | ||
g bool = true | ||
) | ||
fmt.Println(d, e, g) | ||
} | ||
``` | ||
|
||
</div> | ||
</details> | ||
|
||
2. 下列代码的输出是: | ||
|
||
```go | ||
func main() { | ||
const N = 100 | ||
var x int = N | ||
|
||
const M int32 = 100 | ||
var y int = M | ||
fmt.Println(x, y) | ||
} | ||
``` | ||
|
||
<details> | ||
<summary>答案</summary> | ||
<div> | ||
|
||
编译失败:cannot use M (type int32) as type int in assignment | ||
|
||
Go 语言中,常量分为无类型常量和有类型常量两种,`const N = 100`,属于无类型常量,赋值给其他变量时,如果字面量能够转换为对应类型的变量,则赋值成功,例如,`var x int = N`。但是对于有类型的常量 `const M int32 = 100`,赋值给其他变量时,需要类型匹配才能成功,所以显示地类型转换: | ||
|
||
```go | ||
var y int = int(M) | ||
``` | ||
|
||
</div> | ||
</details> | ||
|
||
|
||
3. 下列代码的输出是: | ||
|
||
```go | ||
func main() { | ||
var a int8 = -1 | ||
var b int8 = -128 / a | ||
fmt.Println(b) | ||
} | ||
``` | ||
|
||
<details> | ||
<summary>答案</summary> | ||
<div> | ||
|
||
-128 | ||
|
||
int8 能表示的数字的范围是 [-2^7, 2^7-1],即 [-128, 127]。-128 是无类型常量,转换为 int8,再除以变量 -1,结果为 128,常量除以变量,结果是一个变量。变量转换时允许溢出,符号位变为1,转为补码后恰好等于 -128。 | ||
|
||
对于有符号整型,最高位是是符号位,计算机用补码表示负数。补码 = 原码取反加一。 | ||
|
||
例如: | ||
|
||
```bash | ||
-1 : 11111111 | ||
00000001(原码) 11111110(取反) 11111111(加一) | ||
-128: | ||
10000000(原码) 01111111(取反) 10000000(加一) | ||
|
||
-1 + 1 = 0 | ||
11111111 + 00000001 = 00000000(最高位溢出省略) | ||
-128 + 127 = -1 | ||
10000000 + 01111111 = 11111111 | ||
``` | ||
|
||
</div> | ||
</details> | ||
|
||
4. 下列代码的输出是: | ||
|
||
```go | ||
func main() { | ||
const a int8 = -1 | ||
var b int8 = -128 / a | ||
fmt.Println(b) | ||
} | ||
``` | ||
|
||
<details> | ||
<summary>答案</summary> | ||
<div> | ||
|
||
编译失败:constant 128 overflows int8 | ||
|
||
-128 和 a 都是常量,在编译时求值,-128 / a = 128,两个常量相除,结果也是一个常量,常量类型转换时不允许溢出,因而编译失败。 | ||
|
||
</div> | ||
</details> | ||
|
||
## 作用域 | ||
|
||
1. 下列代码的输出是: | ||
|
||
```go | ||
func main() { | ||
var err error | ||
if err == nil { | ||
err := fmt.Errorf("err") | ||
fmt.Println(1, err) | ||
} | ||
if err != nil { | ||
fmt.Println(2, err) | ||
} | ||
} | ||
``` | ||
|
||
<details> | ||
<summary>答案</summary> | ||
<div> | ||
|
||
1 err | ||
|
||
`:=` 表示声明并赋值,`=` 表示仅赋值。 | ||
|
||
变量的作用域是大括号,因此在第一个 if 语句 `if err == nil` 内部重新声明且赋值了与外部变量同名的局部变量 err。对该局部变量的赋值不会影响到外部的 err。因此第二个 if 语句 `if err != nil` 不成立。所以只打印了 `1 err`。 | ||
|
||
</div> | ||
</details> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters