Add Chapter 2: Go Basics - Variables, Types, and Flow Control (Deep Dive)
This commit is contained in:
3
chapters/chapter-2/go.mod
Normal file
3
chapters/chapter-2/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module go-tutorial
|
||||
|
||||
go 1.21
|
||||
241
chapters/chapter-2/main.go
Normal file
241
chapters/chapter-2/main.go
Normal file
@@ -0,0 +1,241 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 2.1 变量声明示例
|
||||
func variableDeclaration() {
|
||||
fmt.Println("=== 变量声明示例 ===")
|
||||
|
||||
// 1. 显式类型
|
||||
var name string = "Alice"
|
||||
var age int = 25
|
||||
fmt.Printf("name: %s, age: %d\n", name, age)
|
||||
|
||||
// 2. 类型推断
|
||||
var city = "Beijing"
|
||||
var score = 95.5
|
||||
fmt.Printf("city: %s, score: %.1f\n", city, score)
|
||||
|
||||
// 3. 短声明
|
||||
x, y, z := 1, 2, 3
|
||||
fmt.Printf("x: %d, y: %d, z: %d\n", x, y, z)
|
||||
|
||||
// 4. 多变量声明
|
||||
var (
|
||||
pi = 3.14159
|
||||
euler = 2.71828
|
||||
golden = 1.61803
|
||||
)
|
||||
fmt.Printf("pi: %.5f, e: %.5f, golden: %.5f\n", pi, euler, golden)
|
||||
}
|
||||
|
||||
// 2.2 零值示例
|
||||
func zeroValues() {
|
||||
fmt.Println("\n=== 零值示例 ===")
|
||||
|
||||
var intVar int
|
||||
var floatVar float64
|
||||
var boolVar bool
|
||||
var stringVar string
|
||||
var ptrVar *int
|
||||
|
||||
fmt.Printf("int: %d\n", intVar)
|
||||
fmt.Printf("float: %f\n", floatVar)
|
||||
fmt.Printf("bool: %v\n", boolVar)
|
||||
fmt.Printf("string: '%s'\n", stringVar)
|
||||
fmt.Printf("ptr: %v\n", ptrVar)
|
||||
|
||||
// 结构体零值
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
Email string
|
||||
}
|
||||
u := User{}
|
||||
fmt.Printf("User: %+v\n", u)
|
||||
}
|
||||
|
||||
// 2.3 数据类型示例
|
||||
func dataTypes() {
|
||||
fmt.Println("\n=== 数据类型示例 ===")
|
||||
|
||||
// 整型
|
||||
var i8 int8 = 127
|
||||
var i64 int64 = 9223372036854775807
|
||||
fmt.Printf("int8: %d, int64: %d\n", i8, i64)
|
||||
|
||||
// byte 和 rune
|
||||
var b byte = 'A'
|
||||
var r rune = '中'
|
||||
fmt.Printf("byte: %c (%d), rune: %c (%d)\n", b, b, r, r)
|
||||
|
||||
// 浮点数精度问题
|
||||
a := 0.1 + 0.2
|
||||
b := 0.3
|
||||
fmt.Printf("0.1 + 0.2 == 0.3: %v\n", a == b)
|
||||
fmt.Printf("0.1 + 0.2 = %.20f\n", a)
|
||||
|
||||
// 字符串
|
||||
s := "Hello 世界"
|
||||
fmt.Printf("字符串: %s\n", s)
|
||||
fmt.Printf("字节数: %d\n", len(s))
|
||||
|
||||
// 遍历字符串
|
||||
fmt.Print("字符遍历: ")
|
||||
for i, r := range s {
|
||||
fmt.Printf("[%d]%c ", i, r)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 2.4 类型转换示例
|
||||
func typeConversion() {
|
||||
fmt.Println("\n=== 类型转换示例 ===")
|
||||
|
||||
var f float64 = 3.14159
|
||||
var i int = int(f) // 截断
|
||||
fmt.Printf("float64(3.14159) -> int: %d\n", i)
|
||||
|
||||
// 四舍五入
|
||||
i = int(math.Round(f))
|
||||
fmt.Printf("round(3.14159): %d\n", i)
|
||||
|
||||
// 字符串转数字
|
||||
numStr := "42"
|
||||
num, _ := strconv.Atoi(numStr)
|
||||
fmt.Printf("字符串 '%s' -> 数字: %d\n", numStr, num)
|
||||
}
|
||||
|
||||
// 2.5 运算符示例
|
||||
func operators() {
|
||||
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 = %.2f (浮点除法)\n", a, b, float64(a)/float64(b))
|
||||
|
||||
// 位运算
|
||||
x, y := 6, 3 // 110, 011
|
||||
fmt.Printf("%d & %d = %d\n", x, y, x&y)
|
||||
fmt.Printf("%d | %d = %d\n", x, y, x|y)
|
||||
fmt.Printf("%d ^ %d = %d\n", x, y, x^y)
|
||||
fmt.Printf("%d << 1 = %d\n", x, x<<1)
|
||||
}
|
||||
|
||||
// 2.6 流程控制示例
|
||||
func flowControl() {
|
||||
fmt.Println("\n=== 流程控制示例 ===")
|
||||
|
||||
// if-else
|
||||
score := 85
|
||||
if score >= 90 {
|
||||
fmt.Println("优秀")
|
||||
} else if score >= 80 {
|
||||
fmt.Println("良好")
|
||||
} else {
|
||||
fmt.Println("及格")
|
||||
}
|
||||
|
||||
// switch
|
||||
day := 3
|
||||
switch day {
|
||||
case 1, 2, 3, 4, 5:
|
||||
fmt.Println("工作日")
|
||||
case 6, 7:
|
||||
fmt.Println("周末")
|
||||
default:
|
||||
fmt.Println("无效日期")
|
||||
}
|
||||
|
||||
// for 循环
|
||||
fmt.Print("for 循环: ")
|
||||
for i := 0; i < 5; i++ {
|
||||
fmt.Printf("%d ", i)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
// range 遍历
|
||||
slice := []int{10, 20, 30, 40, 50}
|
||||
fmt.Print("range 遍历: ")
|
||||
for i, v := range slice {
|
||||
fmt.Printf("[%d]=%d ", i, v)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 2.7 综合案例:计算器
|
||||
func calculator() {
|
||||
fmt.Println("\n=== 简易计算器 ===")
|
||||
|
||||
num1 := 10.5
|
||||
num2 := 3.2
|
||||
operators := []string{"+", "-", "*", "/"}
|
||||
|
||||
for _, op := range operators {
|
||||
var result float64
|
||||
switch op {
|
||||
case "+":
|
||||
result = num1 + num2
|
||||
case "-":
|
||||
result = num1 - num2
|
||||
case "*":
|
||||
result = num1 * num2
|
||||
case "/":
|
||||
result = num1 / num2
|
||||
}
|
||||
fmt.Printf("%.1f %s %.1f = %.2f\n", num1, op, num2, result)
|
||||
}
|
||||
}
|
||||
|
||||
// 2.7 综合案例:素数判断
|
||||
func primeNumbers() {
|
||||
fmt.Println("\n=== 素数统计 ===")
|
||||
|
||||
count := 0
|
||||
for i := 2; i <= 100; i++ {
|
||||
if isPrime(i) {
|
||||
fmt.Printf("%d ", i)
|
||||
count++
|
||||
if count%10 == 0 {
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Printf("\n1-100 共有 %d 个素数\n", count)
|
||||
}
|
||||
|
||||
func isPrime(n int) bool {
|
||||
if n <= 1 {
|
||||
return false
|
||||
}
|
||||
if n == 2 {
|
||||
return true
|
||||
}
|
||||
if n%2 == 0 {
|
||||
return false
|
||||
}
|
||||
for i := 3; i*i <= n; i += 2 {
|
||||
if n%i == 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func main() {
|
||||
variableDeclaration()
|
||||
zeroValues()
|
||||
dataTypes()
|
||||
typeConversion()
|
||||
operators()
|
||||
flowControl()
|
||||
calculator()
|
||||
primeNumbers()
|
||||
}
|
||||
Reference in New Issue
Block a user