Skip to content

Commit

Permalink
qa-golang add code output page
Browse files Browse the repository at this point in the history
  • Loading branch information
geektutu committed Oct 10, 2020
1 parent 243dd29 commit 3486e6b
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [Go 语言笔试面试题(基础语法)](https://geektutu.com/post/qa-golang-1.html)
- [Go 语言笔试面试题(实现原理)](https://geektutu.com/post/qa-golang-2.html)
- [Go 语言笔试面试题(并发编程)](https://geektutu.com/post/qa-golang-3.html)
- [Go 语言笔试面试题(代码输出)](https://geektutu.com/post/qa-golang-c1.html)

## 相关链接

Expand Down
3 changes: 2 additions & 1 deletion qa-golang/qa-golang-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ func main() {
}
```

`foo()` 函数中,如果 v 分配在栈上,foo 函数返回时,`&v` 就不存在了,但是这段函数是能够正常运行的。Go 编译器发现 v 的引用脱离了 foo 的作用域,会将其分配在堆上。因此,main 函数中仍能够正常访问该值。

</div>
</details>

`foo()` 函数中,如果 v 分配在栈上,foo 函数返回时,`&v` 就不存在了,但是这段函数是能够正常运行的。Go 编译器发现 v 的引用脱离了 foo 的作用域,会将其分配在堆上。因此,main 函数中仍能够正常访问该值。

## Q3 2 个 interface 可以比较吗?

Expand Down
176 changes: 176 additions & 0 deletions qa-golang/qa-golang-c1.md
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
---

![golang interview questions](qa-golang/go_questions.jpg)

**[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>


8 changes: 6 additions & 2 deletions qa-golang/qa-golang.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ nav: 面试
categories:
- Go 语言面试题
image: post/qa-golang/go_questions.jpg
top: 3
github: https://github.com/geektutu/interview-questions
---

Expand Down Expand Up @@ -50,4 +49,9 @@ github: https://github.com/geektutu/interview-questions

- 01 无缓冲的 channel 和有缓冲的 channel 的区别?
- 02 什么是协程泄露(Goroutine Leak)?
- 03 Go 可以限制运行时操作系统线程的数量吗?
- 03 Go 可以限制运行时操作系统线程的数量吗?

## [代码输出](https://geektutu.com/post/qa-golang-c1.html)

- 变量与常量
- 作用域

0 comments on commit 3486e6b

Please sign in to comment.