Update
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
# 02-constants - 常量与iota
|
||||
|
||||
## 知识点讲解
|
||||
|
||||
### 常量定义
|
||||
|
||||
常量使用`const`关键字定义,值在编译时确定且不可修改。
|
||||
|
||||
**基本语法**:
|
||||
```go
|
||||
const PI = 3.14159
|
||||
const MAX_SIZE = 100
|
||||
const greeting = "Hello"
|
||||
```
|
||||
|
||||
**批量声明**:
|
||||
```go
|
||||
const (
|
||||
STATUS_OK = 200
|
||||
STATUS_NOT_FOUND = 404
|
||||
STATUS_ERROR = 500
|
||||
)
|
||||
```
|
||||
|
||||
### iota常量生成器
|
||||
|
||||
`iota`是Go语言的特殊常量,在const声明块中,每增加一行`iota`的值自增1(从0开始)。
|
||||
|
||||
**基本用法**:
|
||||
```go
|
||||
const (
|
||||
A = iota // 0
|
||||
B = iota // 1
|
||||
C = iota // 2
|
||||
)
|
||||
```
|
||||
|
||||
**简化写法**(首个表达式后后续行省略):
|
||||
```go
|
||||
const (
|
||||
A = iota // 0
|
||||
B // 1
|
||||
C // 2
|
||||
)
|
||||
```
|
||||
|
||||
**表达式模式**:
|
||||
```go
|
||||
const (
|
||||
KB = 1 << (10 * iota) // 1 << 0 = 1
|
||||
MB // 1 << 10 = 1024
|
||||
GB // 1 << 20 = 1048576
|
||||
TB // 1 << 30 = 1073741824
|
||||
)
|
||||
```
|
||||
|
||||
### 重要提示
|
||||
|
||||
- 常量只能是基本类型:数字、字符串、布尔值
|
||||
- 常量必须初始化,无法被赋值
|
||||
- `iota`只可用于const块内
|
||||
- const块内,每个单独的const声明会重置iota为0
|
||||
|
||||
## 学习目标
|
||||
|
||||
完成本练习后,你将:
|
||||
- ✅ 掌握常量的定义方式
|
||||
- ✅ 理解iota的工作原理
|
||||
- ✅ 能够使用iota创建枚举值
|
||||
- ✅ 掌握iota的高级用法(表达式模式)
|
||||
|
||||
## 练习说明
|
||||
|
||||
打开 `main.go`,根据注释提示填充代码,完成后运行 `go test -v` 验证答案。
|
||||
@@ -0,0 +1,99 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func exercise1() {
|
||||
fmt.Println("=== 练习1:基本常量定义 ===")
|
||||
|
||||
// 使用const定义以下常量:
|
||||
// GREETING: string,值为"你好,世界!"
|
||||
// MAX_COUNT: int,值为1000
|
||||
// SUCCESS: bool,值为true
|
||||
const GREETING string = ____
|
||||
const MAX_COUNT int = ____
|
||||
const SUCCESS bool = ____
|
||||
|
||||
fmt.Printf("问候语: %s\n最大数量: %d\n成功状态: %t\n", GREETING, MAX_COUNT, SUCCESS)
|
||||
}
|
||||
|
||||
func exercise2() {
|
||||
fmt.Println("\n=== 练习2:批量常量定义 ===")
|
||||
|
||||
// 使用const块批量定义以下常量:
|
||||
// HTTP_OK: int,值为200
|
||||
// HTTP_NOT_FOUND: int,值为404
|
||||
// HTTP_SERVER_ERROR: int,值为500
|
||||
const (
|
||||
HTTP_OK = ____
|
||||
HTTP_NOT_FOUND = ____
|
||||
HTTP_SERVER_ERROR = ____
|
||||
)
|
||||
|
||||
fmt.Printf("HTTP状态码: OK=%d, NOT_FOUND=%d, SERVER_ERROR=%d\n",
|
||||
HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR)
|
||||
}
|
||||
|
||||
func exercise3() {
|
||||
fmt.Println("\n=== 练习3:iota基础用法 ===")
|
||||
|
||||
// 使用iota定义一组连续的整数常量:
|
||||
// SPRING: 0
|
||||
// SUMMER: 1
|
||||
// AUTUMN: 2
|
||||
// WINTER: 3
|
||||
// 提示:第一个使用 ioda = ioda,后续行省略
|
||||
const (
|
||||
SPRING = iota
|
||||
SUMMER
|
||||
AUTUMN
|
||||
WINTER
|
||||
)
|
||||
|
||||
fmt.Printf("季节常量: SPRING=%d, SUMMER=%d, AUTUMN=%d, WINTER=%d\n",
|
||||
SPRING, SUMMER, AUTUMN, WINTER)
|
||||
}
|
||||
|
||||
func exercise4() {
|
||||
fmt.Println("\n=== 练习4:iota带表达式 ===")
|
||||
|
||||
// 使用iota定义从100开始的连续整数:
|
||||
// LEVEL_1: 100
|
||||
// LEVEL_2: 101
|
||||
// LEVEL_3: 102
|
||||
// LEVEL_4: 103
|
||||
// 提示:第一个常量使用 ioda + 100
|
||||
const (
|
||||
LEVEL_1 = iota + ____
|
||||
LEVEL_2
|
||||
LEVEL_3
|
||||
LEVEL_4
|
||||
)
|
||||
|
||||
fmt.Printf("等级常量: LEVEL_1=%d, LEVEL_2=%d, LEVEL_3=%d, LEVEL_4=%d\n",
|
||||
LEVEL_1, LEVEL_2, LEVEL_3, LEVEL_4)
|
||||
}
|
||||
|
||||
func exercise5() {
|
||||
fmt.Println("\n=== 练习5:iota位运算模式 ===")
|
||||
|
||||
// 使用iota和位移操作定义单位换算:
|
||||
// KB: 1024 (1 << 10)
|
||||
// MB: 1048576 (1 << 20)
|
||||
// GB: 1073741824 (1 << 30)
|
||||
const (
|
||||
_ = iota
|
||||
KB = 1 << ____
|
||||
MB = 1 << ____
|
||||
GB = 1 << ____
|
||||
)
|
||||
|
||||
fmt.Printf("存储单位: KB=%d, MB=%d, GB=%d\n", KB, MB, GB)
|
||||
}
|
||||
|
||||
func main() {
|
||||
exercise1()
|
||||
exercise2()
|
||||
exercise3()
|
||||
exercise4()
|
||||
exercise5()
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func captureOutput(f func()) string {
|
||||
old := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
f()
|
||||
|
||||
w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func TestExercise1(t *testing.T) {
|
||||
output := captureOutput(exercise1)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected string
|
||||
}{
|
||||
{"问候语检查", "问候语: 你好,世界!"},
|
||||
{"最大数量检查", "最大数量: 1000"},
|
||||
{"成功状态检查", "成功状态: true"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if !strings.Contains(output, tt.expected) {
|
||||
t.Errorf("期望输出包含 '%s', 但实际输出: %s", tt.expected, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise2(t *testing.T) {
|
||||
output := captureOutput(exercise2)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected string
|
||||
}{
|
||||
{"HTTP_OK检查", "HTTP状态码: OK=200, NOT_FOUND=404"},
|
||||
{"HTTP_SERVER_ERROR检查", "SERVER_ERROR=500"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if !strings.Contains(output, tt.expected) {
|
||||
t.Errorf("期望输出包含 '%s', 但实际输出: %s", tt.expected, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise3(t *testing.T) {
|
||||
output := captureOutput(exercise3)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected string
|
||||
}{
|
||||
{"季节常量检查", "季节常量: SPRING=0, SUMMER=1, AUTUMN=2, WINTER=3"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if !strings.Contains(output, tt.expected) {
|
||||
t.Errorf("期望输出包含 '%s', 但实际输出: %s", tt.expected, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise4(t *testing.T) {
|
||||
output := captureOutput(exercise4)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected string
|
||||
}{
|
||||
{"等级常量检查", "等级常量: LEVEL_1=100, LEVEL_2=101, LEVEL_3=102, LEVEL_4=103"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if !strings.Contains(output, tt.expected) {
|
||||
t.Errorf("期望输出包含 '%s', 但实际输出: %s", tt.expected, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise5(t *testing.T) {
|
||||
output := captureOutput(exercise5)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected string
|
||||
}{
|
||||
{"存储单位检查", "存储单位: KB=1024, MB=1048576, GB=1073741824"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if !strings.Contains(output, tt.expected) {
|
||||
t.Errorf("期望输出包含 '%s', 但实际输出: %s", tt.expected, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainFunction(t *testing.T) {
|
||||
output := captureOutput(main)
|
||||
|
||||
requiredSections := []string{
|
||||
"练习1:基本常量定义",
|
||||
"练习2:批量常量定义",
|
||||
"练习3:iota基础用法",
|
||||
"练习4:iota带表达式",
|
||||
"练习5:iota位运算模式",
|
||||
}
|
||||
|
||||
for _, section := range requiredSections {
|
||||
if !strings.Contains(output, section) {
|
||||
t.Errorf("期望输出包含 '%s'", section)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user