package main
import (
"encoding/json"
"fmt"
"os"
"reflect"
"runtime"
)
type CaseResult struct {
Name string `json:"name"`
Summary string `json:"summary"`
Expected any `json:"expected"`
Actual any `json:"actual"`
Passed bool `json:"passed"`
}
type LabOutput struct {
Version string `json:"version"`
Cases []CaseResult `json:"cases"`
}
func makeCase(name, summary string, expected, actual any) CaseResult {
return CaseResult{
Name: name,
Summary: summary,
Expected: expected,
Actual: actual,
Passed: reflect.DeepEqual(expected, actual),
}
}
func swapPtr(x, y *int) {
*x, *y = *y, *x
}
func main() {
var cases []CaseResult
// 1. address_of_operator
var a int = 10
hexAddr := fmt.Sprintf("%x", &a)
cases = append(cases, makeCase(
"address_of_operator",
"验证取地址运算符 & 返回非空 *int 内存地址并可用 %x 格式化",
map[string]any{
"ptr_type": "*int",
"addr_non_nil": true,
"hex_non_zero": true,
},
map[string]any{
"ptr_type": fmt.Sprintf("%T", &a),
"addr_non_nil": &a != nil,
"hex_non_zero": len(hexAddr) > 0 && hexAddr != "0",
},
))
// 2. pointer_decl_assign_deref
var b int = 20
var ip *int
var fp *float32
ip = &b
beforeDeref := *ip
*ip = 35
cases = append(cases, makeCase(
"pointer_decl_assign_deref",
"验证指针声明、赋值 &b、*ip 解引用读取 20 以及经由 *ip 修改原变量为 35",
map[string]any{
"ip_eq_addr_b": true,
"fp_type": "*float32",
"before_deref": 20,
"b_after_write": 35,
},
map[string]any{
"ip_eq_addr_b": ip == &b,
"fp_type": fmt.Sprintf("%T", fp),
"before_deref": beforeDeref,
"b_after_write": b,
},
))
// 3. nil_pointer_check
var ptr *int
cases = append(cases, makeCase(
"nil_pointer_check",
"验证未初始化空指针值为 nil、%x 格式化输出 0 且支持 == nil / != nil 判断",
map[string]any{
"is_nil": true,
"not_nil": false,
"hex_format": "0",
},
map[string]any{
"is_nil": ptr == nil,
"not_nil": ptr != nil,
"hex_format": fmt.Sprintf("%x", ptr),
},
))
// 4. pointer_array
nums := [3]int{10, 100, 200}
var ptrArr [3]*int
for i := 0; i < 3; i++ {
ptrArr[i] = &nums[i]
}
var derefed []int
for i := 0; i < 3; i++ {
derefed = append(derefed, *ptrArr[i])
}
cases = append(cases, makeCase(
"pointer_array",
"验证定长指针数组 [3]*int 存储各元素地址并解引用还原 [10, 100, 200]",
[]int{10, 100, 200},
derefed,
))
// 5. pointer_to_pointer_and_param
v := 3000
p := &v
pp := &p
x, y := 100, 200
swapPtr(&x, &y)
cases = append(cases, makeCase(
"pointer_to_pointer_and_param",
"验证二级指针 **int 解引用读取 3000 以及函数指针参数交换实参 (100,200) -> (200,100)",
map[string]any{
"deref_p": 3000,
"deref_pp": 3000,
"swapped": []int{200, 100},
},
map[string]any{
"deref_p": *p,
"deref_pp": **pp,
"swapped": []int{x, y},
},
))
out := LabOutput{
Version: fmt.Sprintf("%s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH),
Cases: cases,
}
enc := json.NewEncoder(os.Stdout)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
if err := enc.Encode(out); err != nil {
panic(err)
}
}
本例需在 本地离线 Docker 容器(golang:1.22-alpine,go1.22.12 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(golang:1.22-alpine,go1.22.12 linux/amd64,--network none)。
入口文件:projects/go-pointers-lab/explore_pointers.go
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore_pointers.go,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(golang:1.22-alpine,go1.22.12 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联33章) · 含当前章节或实例 · mbskycom202612a1159.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1178.zip