蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 Go 六大类运算符、位运算真值表、复合赋值序列与 5 级优先级规则

GO本地离线 Docker 容器(golang:1.22-alpine,go1.22.12 linux/amd64,--network none)。explore_operators.go
explore_operators.go223 行
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 main() {
	var cases []CaseResult

	// 1. arithmetic_operators
	a, b := 21, 10
	incA := a
	incA++
	decA := a
	decA--
	ta, tb := 10, 20
	taInc := ta
	taInc++
	taDec := ta
	taDec--
	cases = append(cases, makeCase(
		"arithmetic_operators",
		"验证 +、-、*、/、%、++、-- 算术运算符在参考表样例 (10,20) 与示例 (21,10) 下的结果",
		map[string]any{
			"example": []int{31, 11, 210, 2, 1, 22, 20},
			"table":   []int{30, -10, 200, 2, 0, 11, 9},
		},
		map[string]any{
			"example": []int{a + b, a - b, a * b, a / b, a % b, incA, decA},
			"table":   []int{ta + tb, ta - tb, ta * tb, tb / ta, tb % ta, taInc, taDec},
		},
	))

	// 2. relational_operators
	cases = append(cases, makeCase(
		"relational_operators",
		"验证 ==、!=、>、<、>=、<= 关系运算符返回布尔值",
		map[string]any{
			"eq_21_10": false,
			"ne_21_10": true,
			"lt_21_10": false,
			"gt_21_10": true,
			"le_5_20":  true,
			"ge_20_5":  true,
		},
		map[string]any{
			"eq_21_10": 21 == 10,
			"ne_21_10": 21 != 10,
			"lt_21_10": 21 < 10,
			"gt_21_10": 21 > 10,
			"le_5_20":  5 <= 20,
			"ge_20_5":  20 >= 5,
		},
	))

	// 3. logical_operators
	la, lb := true, false
	and1 := la && lb
	or1 := la || lb
	la, lb = false, true
	and2 := la && lb
	notAnd2 := !(la && lb)
	cases = append(cases, makeCase(
		"logical_operators",
		"验证 &&、||、! 逻辑运算符组合真值",
		[]bool{false, true, false, true},
		[]bool{and1, or1, and2, notAnd2},
	))

	// 4. bitwise_operators
	truthPairs := [][2]uint{{0, 0}, {0, 1}, {1, 1}, {1, 0}}
	var truthTable [][]int
	for _, pr := range truthPairs {
		p, q := pr[0], pr[1]
		truthTable = append(truthTable, []int{int(p), int(q), int(p & q), int(p | q), int(p ^ q)})
	}
	var ba uint = 60
	var bb uint = 13
	cases = append(cases, makeCase(
		"bitwise_operators",
		"验证按位真值表以及 60 与 13 的 &、|、^、<<、>> 与位清除 &^ 运算结果",
		map[string]any{
			"truth": [][]int{
				{0, 0, 0, 0, 0},
				{0, 1, 0, 1, 1},
				{1, 1, 1, 1, 0},
				{1, 0, 0, 1, 1},
			},
			"and":    12,
			"or":     61,
			"xor":    49,
			"lshift": 240,
			"rshift": 15,
			"bitclr": 48,
		},
		map[string]any{
			"truth":  truthTable,
			"and":    int(ba & bb),
			"or":     int(ba | bb),
			"xor":    int(ba ^ bb),
			"lshift": int(ba << 2),
			"rshift": int(ba >> 2),
			"bitclr": int(ba &^ bb),
		},
	))

	// 5. assignment_operators
	aa := 21
	c := aa
	r1 := c
	c += aa
	r2 := c
	c -= aa
	r3 := c
	c *= aa
	r4 := c
	c /= aa
	r5 := c
	remC := 200
	remC %= 30
	c = 200
	c <<= 2
	r6 := c
	c >>= 2
	r7 := c
	c &= 2
	r8 := c
	c ^= 2
	r9 := c
	c |= 2
	r10 := c
	cases = append(cases, makeCase(
		"assignment_operators",
		"验证 11 种简单与复合赋值运算符逐步更新变量的结果序列",
		map[string]any{
			"seq":    []int{21, 42, 21, 441, 21, 800, 200, 0, 2, 2},
			"mod_eq": 20,
		},
		map[string]any{
			"seq":    []int{r1, r2, r3, r4, r5, r6, r7, r8, r9, r10},
			"mod_eq": remC,
		},
	))

	// 6. address_and_pointer_operators
	var pa int = 4
	var pb int32
	var pc float32
	var ptr *int = &pa
	cases = append(cases, makeCase(
		"address_and_pointer_operators",
		"验证 %T 类型输出以及 & 取地址与 * 解引用运算符",
		map[string]any{
			"types":     []string{"int", "int32", "float32"},
			"a_val":     4,
			"deref_val": 4,
		},
		map[string]any{
			"types":     []string{fmt.Sprintf("%T", pa), fmt.Sprintf("%T", pb), fmt.Sprintf("%T", pc)},
			"a_val":     pa,
			"deref_val": *ptr,
		},
	))

	// 7. operator_precedence
	p1, p2, p3, p4 := 20, 10, 15, 5
	e1 := (p1 + p2) * p3 / p4
	e2 := ((p1 + p2) * p3) / p4
	e3 := (p1 + p2) * (p3 / p4)
	e4 := p1 + (p2*p3)/p4
	precCheck := (2+3*4 == 14) && (1+2 == 3 && 4 < 5 || false)
	cases = append(cases, makeCase(
		"operator_precedence",
		"验证括号改变运算优先级以及 Go 5 级二元运算符优先级规则",
		map[string]any{
			"exprs":      []int{90, 90, 90, 50},
			"prec_check": true,
		},
		map[string]any{
			"exprs":      []int{e1, e2, e3, e4},
			"prec_check": precCheck,
		},
	))

	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-operators-lab/explore_operators.go

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore_operators.go,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(golang:1.22-alpine,go1.22.12 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Go

课程关联工程包(登记关联33章) · 含当前章节或实例 · mbskycom202612a1159.zip

下载课程关联工程包:Go

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1177.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习