go/demo/demo_std.go · 5067 字节 · 下载原文
// demo_std.go —— **标准输出**示范(Go)
// demo_std.go -- the **standard output** demo (Go)
//
// 四层示范的第一层:只用这门语言自己的标准输出,**不读输入、不画图、不弹窗**,
// The first of the four demo layers: use only this language's own standard output, **no reading input, no drawing, no dialogs**,
// 输出**逐字节确定**,能正常结束。
// the output is **byte-for-byte deterministic**, and it exits normally.
//
// ◆ 写法(Go 前端的两条实测约束)
// ◆ Style (two measured constraints of the Go frontend)
//
// · `println` 多个实参之间**自动补一个空格**(`println("a=", 7)` → `a= 7`),
// · `println` **inserts one space between multiple arguments automatically** (`println("a=", 7)` -> `a= 7`),
// 所以拼"标签 + 值"时标签末尾不要多写空格。
// so when building "label + value" do not add a trailing space to the label.
// · **字符串拼接在本前端编出来是空串**(`"a" + "b"` 渲染出来一个字都没有,
// · **String concatenation compiles to an empty string on this frontend** (`"a" + "b"` renders nothing at all,
// 见 `snake.go` 文件头)⇒ 这一份全程不拼字符串,要拼的地方直接并列成多个实参。
// see the `snake.go` file header) => this file never concatenates strings; where concatenation would be needed it just lists several arguments.
// · `\x1b` 转义**不解析**(原样打出 `x1b`)⇒ 控制字符走 `putchar(码)`,见 `demo_tty.go`。
// · The `\x1b` escape is **not parsed** (it prints `x1b` verbatim) => emit control characters via `putchar(code)`, see `demo_tty.go`.
// ⚠ **2026-10-04 复验仍成立**:三条都原样成立 —— `println("a=", 7)` 打出 `a= 7`;`"a" + "b"` 渲染成空(trace-draw `str=""`);
// ⚠ **still holds on 2026-10-04**: all three hold unchanged -- `println("a=", 7)` prints `a= 7`; `"a" + "b"` renders empty (trace-draw `str=""`);
// `println("X\x1bY")` 打出 `Xx1bY`。
// `println("X\x1bY")` prints `Xx1bY`.
//
// 跑法:命令行页输入 vml run examples/go/demo_std.go
// How to run: type this into the command-line page: vml run examples/go/demo_std.go
package main
// 显式引用 UI 库:`import "vmlui"` 会**真的去读** `Lib/go/vmlui/vmlui.go`
// Explicitly reference the UI library: `import "vmlui"` actually reads `Lib/go/vmlui/vmlui.go`
// (GenLib 生成、155 个 `ui_*` 声明),把符号登记进编译器的符号表。
// (generated by GenLib, 155 `ui_*` declarations), registering those symbols in the compiler's symbol table.
//
// ⚠ 删掉这一行就**编不过** —— 判据(实测):
// ⚠ Removing this line **fails to compile** - the measured criterion:
// vmlcli <本文件> ⇒ error: 未声明的函数 'ui_win_open'(它属于包 'vmlui'…)
// vmlcli <this file> => error: undeclared function 'ui_win_open' (it belongs to package 'vmlui' ...)
// 所以它是**强制**的,不是文档级的摆设。
// so it is **mandatory**, not documentation-only.
// ⚠ **2026-10-04 复验仍成立**:/tmp 副本删掉这行后实测 `✘ 编译失败`(本文件报「未声明的函数 'ui_scr_get_language'」)。
// ⚠ **still holds on 2026-10-04**: a /tmp copy with this line removed measured `✘ compile failed` (this file reports 「undefined function 'ui_scr_get_language'」).
import "vmlui"
func main() {
lang := ui_scr_get_language()
println("=== Dolaima demo_std (Go) ===")
// ① 字符串
// ① String
if lang == 0 { println("[字符串] hello, world") } else { println("[string] hello, world") }
// ② 整数
// ② Integer
n := 42
if lang == 0 { println("[整数] n =", n) } else { println("[int] n =", n) }
// ③ 计算结果
// ③ Computed result
if lang == 0 { println("[计算] 6 * 7 =", 6*7) } else { println("[calc] 6 * 7 =", 6*7) }
a := 7
b := 5
if lang == 0 { println("[计算] a + b =", a+b) } else { println("[calc] a + b =", a+b) }
if lang == 0 { println("[计算] a * b - 3 =", a*b-3) } else { println("[calc] a * b - 3 =", a*b-3) }
// ④ 循环里算斐波那契前 10 项
// ④ Compute the first 10 Fibonacci numbers in a loop
if lang == 0 { println("[循环] 斐波那契前 10 项:") } else { println("[loop] first 10 Fibonacci numbers:") }
x := 0
y := 1
i := 0
for i < 10 {
z := x + y
x = y
y = z
println(" fib =", x)
i = i + 1
}
// ⑤ 累加 1+2+…+100
// ⑤ Accumulate 1+2+...+100
sum := 0
k := 1
for k <= 100 {
sum = sum + k
k = k + 1
}
if lang == 0 { println("[累加] 1+2+...+100 =", sum) } else { println("[sum] 1+2+...+100 =", sum) }
// ⑥ 阶乘 5!
// ⑥ Factorial 5!
fact := 1
m := 1
for m <= 5 {
fact = fact * m
m = m + 1
}
if lang == 0 { println("[阶乘] 5! =", fact) } else { println("[factorial] 5! =", fact) }
if lang == 0 { println("=== 结束 ===") } else { println("=== done ===") }
}