From 7dcc3564c93a4ee2471f6dace4aa2bfb695734bc Mon Sep 17 00:00:00 2001 From: liangzejun Date: Wed, 31 Dec 2025 17:58:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01-basics/variables-demo/go.mod | 3 + 01-basics/variables-demo/main.go | 133 +++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 01-basics/variables-demo/go.mod create mode 100644 01-basics/variables-demo/main.go diff --git a/01-basics/variables-demo/go.mod b/01-basics/variables-demo/go.mod new file mode 100644 index 0000000..fb4d25e --- /dev/null +++ b/01-basics/variables-demo/go.mod @@ -0,0 +1,3 @@ +module variables-demo + +go 1.21 diff --git a/01-basics/variables-demo/main.go b/01-basics/variables-demo/main.go new file mode 100644 index 0000000..d7a7ef4 --- /dev/null +++ b/01-basics/variables-demo/main.go @@ -0,0 +1,133 @@ +package main + +import ( + "fmt" + "strconv" +) + +func main() { + fmt.Println("=== 变量声明示例 ===") + + // 方式1: 标准声明 可以用于包级别声明 + var name string = "张三" + var age int = 25 + + // 方式2: 类型推断 可以用于包级别声明 + var city = "北京" + var score = 95.5 + + // 方式3: 短声明(最常用) 不能用于包级别声明 + email := "test@example.com" + isActive := true + + // 方式4: 批量声明 + var ( + username = "admin" + password = "123456" + port = 8080 + ) + + fmt.Printf("姓名: %s, 年龄: %d, 城市: %s\n", name, age, city) + fmt.Printf("分数: %.2f, 邮箱: %s, 激活: %t\n", score, email, isActive) + fmt.Printf("用户: %s, 端口: %d\n", username, port) + _ = password // 避免未使用变量错误 + + fmt.Println("\n=== 数据类型示例 ===") + + // 整数 + var num int = 100 + var byte1 uint8 = 255 + + // 浮点数 + var pi float64 = 3.141592653589793 + + // 字符串 + var greeting = "Hello, Go!" + multiline := ` + 这是多行字符串 + 保留格式 + ` + + // 布尔值 + var flag bool = true + + fmt.Printf("整数: %d, 字节: %d\n", num, byte1) + fmt.Printf("浮点数: %f\n", pi) + fmt.Printf("字符串: %s\n", greeting) + fmt.Printf("多行: %s\n", multiline) + fmt.Printf("布尔: %t\n", flag) + + fmt.Println("\n=== 常量示例 ===") + + const PI = 3.14159 + const AppName = "MyApp" + + const ( + StatusOK = 200 + StatusNotFound = 404 + StatusError = 500 + ) + + const ( + Monday = iota // 0 + Tuesday // 1 + Wednesday // 2 + Thursday // 3 + Friday // 4 + ) + + fmt.Printf("常量 PI: %f, App: %s\n", PI, AppName) + fmt.Printf("状态码: %d, %d, %d\n", StatusOK, StatusNotFound, StatusError) + fmt.Printf("星期: %d, %d, %d\n", Monday, Tuesday, Wednesday) + + fmt.Println("\n=== 类型转换示例 ===") + + // 数值转换 + var i int = 42 + var f float64 = float64(i) + var u uint = uint(f) + fmt.Printf("int: %d -> float64: %f -> uint: %d\n", i, f, u) + + // 字符串与数值转换 + num2 := 123 + str := strconv.Itoa(num2) + fmt.Printf("int to string: %s (类型: %T)\n", str, str) + + str2 := "456" + num3, err := strconv.Atoi(str2) + if err != nil { + fmt.Println("转换失败:", err) + } else { + fmt.Printf("string to int: %d (类型: %T)\n", num3, num3) + } + + fmt.Println("\n=== 运算符示例 ===") + + a, b := 10, 3 + + fmt.Printf("%d + %d = %d\n", a, b, a+b) + fmt.Printf("%d - %d = %d\n", a, b, a-b) + fmt.Printf("%d * %d = %d\n", a, b, a*b) + fmt.Printf("%d / %d = %d\n", a, b, a/b) + fmt.Printf("%d %% %d = %d\n", a, b, a%b) + + // 比较运算 + fmt.Printf("%d == %d: %t\n", a, b, a == b) + fmt.Printf("%d > %d: %t\n", a, b, a > b) + + // 逻辑运算 + t, f2 := true, false + fmt.Printf("true && false: %t\n", t && f2) + fmt.Printf("true || false: %t\n", t || f2) + fmt.Printf("!true: %t\n", !t) + + fmt.Println("\n=== 零值示例 ===") + + var zeroInt int + var zeroFloat float64 + var zeroBool bool + var zeroString string + + fmt.Printf("int零值: %d, float零值: %f, bool零值: %t, string零值: '%s'\n", + zeroInt, zeroFloat, zeroBool, zeroString) +}