Compare commits
9 Commits
933e6712b2
...
main
Author | SHA1 | Date | |
---|---|---|---|
265eef5627 | |||
4b77e9cfc0 | |||
199e884a55 | |||
3c981f9315 | |||
087e1ba3a1 | |||
ed6444c0ac | |||
6b961ce217 | |||
b1f0de613b | |||
4228f447f7 |
3
01_basics/01_variables/go.mod
Normal file
3
01_basics/01_variables/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module variables
|
||||
|
||||
go 1.23.5
|
16
01_basics/01_variables/main.go
Normal file
16
01_basics/01_variables/main.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// 变量声明与初始化
|
||||
var name string = "Go语言"
|
||||
age := 28 // 类型推断
|
||||
|
||||
// 多变量声明
|
||||
var width, height = 100, 50
|
||||
|
||||
fmt.Println("变量示例:")
|
||||
fmt.Printf("name: %s, age: %d\n", name, age)
|
||||
fmt.Printf("width: %d, height: %d\n", width, height)
|
||||
}
|
3
01_basics/02_data_types/go.mod
Normal file
3
01_basics/02_data_types/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module data_types
|
||||
|
||||
go 1.23.5
|
47
01_basics/02_data_types/main.go
Normal file
47
01_basics/02_data_types/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// main包是Go程序的入口包,每个可执行程序必须包含一个main包
|
||||
package main
|
||||
|
||||
// 导入fmt包,用于格式化输入输出
|
||||
import "fmt"
|
||||
|
||||
// main函数是程序执行的入口点
|
||||
func main() {
|
||||
// 基本数据类型示例
|
||||
// 使用var关键字声明多个变量,可以分组声明
|
||||
var (
|
||||
// int类型表示整数,默认是32位或64位取决于平台
|
||||
integer int = 42
|
||||
// float64表示64位浮点数(双精度浮点数)
|
||||
float float64 = 3.14
|
||||
// bool类型表示布尔值,只有true或false两个值
|
||||
boolean bool = true
|
||||
// string类型表示字符串,使用双引号包裹
|
||||
str string = "Hello"
|
||||
)
|
||||
|
||||
// 复合类型示例
|
||||
// array是固定长度的数组,类型为[3]int表示包含3个int元素的数组
|
||||
var array [3]int = [3]int{1, 2, 3}
|
||||
// slice是动态数组,使用:=短变量声明方式初始化
|
||||
slice := []string{"a", "b", "c"}
|
||||
|
||||
// 打印数据类型示例
|
||||
fmt.Println("数据类型示例:")
|
||||
// 使用fmt.Printf格式化输出:
|
||||
// %d - 整数, %f - 浮点数, %t - 布尔值, %s - 字符串
|
||||
fmt.Printf("integer: %d, float: %f, boolean: %t, string: %s\n",
|
||||
integer, float, boolean, str)
|
||||
// %v - 通用格式,可以打印任何类型的值
|
||||
fmt.Printf("array: %v, slice: %v\n", array, slice)
|
||||
|
||||
// map是键值对集合,这里声明一个string到int的映射
|
||||
var mapExample map[string]int = map[string]int{
|
||||
"one": 1, // 键"one"对应值1
|
||||
"two": 2, // 键"two"对应值2
|
||||
}
|
||||
|
||||
// 从map中获取键"one"对应的值
|
||||
exo := mapExample["one"]
|
||||
// 打印map和从map中获取的值
|
||||
fmt.Printf("mapExample: %v, mapExample[\"one\"]: %d\n", mapExample, exo)
|
||||
}
|
3
01_basics/03_operators/go.mod
Normal file
3
01_basics/03_operators/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module operators
|
||||
|
||||
go 1.23.5
|
29
01_basics/03_operators/main.go
Normal file
29
01_basics/03_operators/main.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a, b := 15, 10
|
||||
|
||||
// 算术运算符
|
||||
sum := a + b
|
||||
diff := a - b
|
||||
product := a * b
|
||||
quotient := a / b
|
||||
remainder := a % b
|
||||
|
||||
// 比较运算符
|
||||
eq := a == b
|
||||
gt := a > b
|
||||
lt := a < b
|
||||
|
||||
fmt.Println("运算符示例:")
|
||||
fmt.Printf("%d + %d = %d\n", a, b, sum)
|
||||
fmt.Printf("%d - %d = %d\n", a, b, diff)
|
||||
fmt.Printf("%d * %d = %d\n", a, b, product)
|
||||
fmt.Printf("%d / %d = %d\n", a, b, quotient)
|
||||
fmt.Printf("%d %% %d = %d\n", a, b, remainder)
|
||||
fmt.Printf("%d == %d: %t\n", a, b, eq)
|
||||
fmt.Printf("%d > %d: %t\n", a, b, gt)
|
||||
fmt.Printf("%d < %d: %t\n", a, b, lt)
|
||||
}
|
3
01_basics/04_control_flow/01_conditionals/go.mod
Normal file
3
01_basics/04_control_flow/01_conditionals/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module conditionals
|
||||
|
||||
go 1.23.5
|
25
01_basics/04_control_flow/01_conditionals/main.go
Normal file
25
01_basics/04_control_flow/01_conditionals/main.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
score := 85
|
||||
|
||||
// if-else条件语句
|
||||
if score >= 90 {
|
||||
fmt.Println("优秀")
|
||||
} else if score >= 80 {
|
||||
fmt.Println("良好")
|
||||
} else if score >= 60 {
|
||||
fmt.Println("及格")
|
||||
} else {
|
||||
fmt.Println("不及格")
|
||||
}
|
||||
|
||||
// 带初始化的if语句
|
||||
if num := 9; num%2 == 0 {
|
||||
fmt.Printf("%d是偶数\n", num)
|
||||
} else {
|
||||
fmt.Printf("%d是奇数\n", num)
|
||||
}
|
||||
}
|
3
01_basics/04_control_flow/02_loops/go.mod
Normal file
3
01_basics/04_control_flow/02_loops/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module loops
|
||||
|
||||
go 1.23.5
|
40
01_basics/04_control_flow/02_loops/main.go
Normal file
40
01_basics/04_control_flow/02_loops/main.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// 基本for循环
|
||||
fmt.Println("基本for循环:")
|
||||
for i := 0; i < 5; i++ {
|
||||
fmt.Printf("%d ", i)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
// while风格的for循环
|
||||
fmt.Println("\nwhile风格循环:")
|
||||
n := 0
|
||||
for n < 3 {
|
||||
fmt.Printf("%d ", n)
|
||||
n++
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
// 无限循环
|
||||
fmt.Println("\n无限循环(按条件break):")
|
||||
count := 0
|
||||
for {
|
||||
if count > 3 {
|
||||
break
|
||||
}
|
||||
fmt.Printf("%d ", count)
|
||||
count++
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
// range循环
|
||||
fmt.Println("\nrange循环:")
|
||||
nums := []int{10, 20, 30}
|
||||
for i, num := range nums {
|
||||
fmt.Printf("索引:%d 值:%d\n", i, num)
|
||||
}
|
||||
}
|
3
01_basics/05_functions/01_definition/go.mod
Normal file
3
01_basics/05_functions/01_definition/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module definition
|
||||
|
||||
go 1.23.5
|
39
01_basics/05_functions/01_definition/main.go
Normal file
39
01_basics/05_functions/01_definition/main.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 基本函数定义
|
||||
func greet(name string) {
|
||||
fmt.Printf("Hello, %s!\n", name)
|
||||
}
|
||||
|
||||
// 带返回值的函数
|
||||
func add(a, b int) int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
// 多返回值函数
|
||||
func swap(x, y string) (string, string) {
|
||||
return y, x
|
||||
}
|
||||
|
||||
// 完成一个两个变量值互换的函数,使用泛型
|
||||
func swapGeneric[T any](x, y T) (T, T) {
|
||||
return y, x
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 调用无返回值函数
|
||||
greet("Gopher")
|
||||
|
||||
// 调用单返回值函数
|
||||
sum := add(3, 5)
|
||||
fmt.Println("3 + 5 =", sum)
|
||||
|
||||
// 调用多返回值函数
|
||||
a, b := swap("hello", "world")
|
||||
fmt.Println(a, b)
|
||||
// 调用泛型函数
|
||||
x, y := swapGeneric(1, 2)
|
||||
fmt.Println(x, y)
|
||||
}
|
3
01_basics/05_functions/02_multiple_returns/go.mod
Normal file
3
01_basics/05_functions/02_multiple_returns/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module multiple_returns
|
||||
|
||||
go 1.23.5
|
39
01_basics/05_functions/02_multiple_returns/main.go
Normal file
39
01_basics/05_functions/02_multiple_returns/main.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
// 返回计算结果和错误信息
|
||||
func sqrt(x float64) (float64, error) {
|
||||
if x < 0 {
|
||||
return 0, fmt.Errorf("不能对负数 %f 开平方", x)
|
||||
}
|
||||
return math.Sqrt(x), nil
|
||||
}
|
||||
|
||||
// 命名返回值
|
||||
func split(sum int) (x, y int) {
|
||||
x = sum * 4 / 9
|
||||
y = sum - x
|
||||
return // 裸返回
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 处理多返回值
|
||||
if result, err := sqrt(16); err != nil {
|
||||
fmt.Println("错误:", err)
|
||||
} else {
|
||||
fmt.Println("平方根:", result)
|
||||
}
|
||||
|
||||
// 处理错误情况
|
||||
if _, err := sqrt(-4); err != nil {
|
||||
fmt.Println("错误:", err)
|
||||
}
|
||||
|
||||
// 使用命名返回值
|
||||
a, b := split(17)
|
||||
fmt.Printf("split(17) = %d, %d\n", a, b)
|
||||
}
|
3
01_basics/05_functions/03_anonymous/go.mod
Normal file
3
01_basics/05_functions/03_anonymous/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module anonymous
|
||||
|
||||
go 1.23.5
|
34
01_basics/05_functions/03_anonymous/main.go
Normal file
34
01_basics/05_functions/03_anonymous/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// 立即执行的匿名函数
|
||||
func(msg string) {
|
||||
fmt.Println(msg)
|
||||
}("立即执行的匿名函数")
|
||||
|
||||
// 将匿名函数赋值给变量
|
||||
greet := func(name string) {
|
||||
fmt.Printf("Hello, %s!\n", name)
|
||||
}
|
||||
greet("Gopher")
|
||||
|
||||
// 闭包示例
|
||||
adder := func() func(int) int {
|
||||
sum := 0
|
||||
return func(x int) int {
|
||||
sum += x
|
||||
return sum
|
||||
}
|
||||
}
|
||||
|
||||
// 使用闭包
|
||||
pos, neg := adder(), adder()
|
||||
for i := 0; i < 5; i++ {
|
||||
fmt.Println(
|
||||
"pos:", pos(i),
|
||||
"neg:", neg(-2*i),
|
||||
)
|
||||
}
|
||||
}
|
3
01_basics/06_packages/go.mod
Normal file
3
01_basics/06_packages/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module packages
|
||||
|
||||
go 1.21
|
13
01_basics/06_packages/main.go
Normal file
13
01_basics/06_packages/main.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"packages/myutils" // 导入自定义包
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 调用外部包中的函数
|
||||
myutils.Greet("Gopher")
|
||||
|
||||
fmt.Println("包导入和使用示例")
|
||||
}
|
14
01_basics/06_packages/myutils/utils.go
Normal file
14
01_basics/06_packages/myutils/utils.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package myutils
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 首字母大写的函数才能被外部包访问
|
||||
func Greet(name string) {
|
||||
internalFunc()
|
||||
fmt.Printf("Hello, %s!\n", name)
|
||||
}
|
||||
|
||||
// 小写函数只能在包内使用
|
||||
func internalFunc() {
|
||||
fmt.Println("This is internal")
|
||||
}
|
21
02_core_concepts/01_pointers/main.go
Normal file
21
02_core_concepts/01_pointers/main.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// 基本指针操作
|
||||
a := 42
|
||||
p := &a // 获取a的地址
|
||||
|
||||
fmt.Printf("a的值: %d, 地址: %p\n", a, p)
|
||||
fmt.Printf("通过指针访问值: %d\n", *p)
|
||||
|
||||
// 通过指针修改值
|
||||
*p = 100
|
||||
fmt.Printf("修改后a的值: %d\n", a)
|
||||
|
||||
// 指针的指针
|
||||
pp := &p
|
||||
fmt.Printf("指针的指针: %p\n", pp)
|
||||
fmt.Printf("通过指针的指针访问值: %d\n", **pp)
|
||||
}
|
3
02_core_concepts/02_structs/go.mod
Normal file
3
02_core_concepts/02_structs/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module structs
|
||||
|
||||
go 1.21
|
53
02_core_concepts/02_structs/main.go
Normal file
53
02_core_concepts/02_structs/main.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 定义一个Person结构体
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
// 给Person结构体定义一个方法
|
||||
func (p Person) Greet() {
|
||||
fmt.Printf("Hello, my name is %s and I'm %d years old.\n", p.Name, p.Age)
|
||||
}
|
||||
|
||||
// 嵌套结构体示例
|
||||
type Employee struct {
|
||||
Person
|
||||
JobTitle string
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 结构体初始化方式1:字段顺序
|
||||
p1 := Person{"Alice", 25}
|
||||
|
||||
// 结构体初始化方式2:字段名指定
|
||||
p2 := Person{
|
||||
Name: "Bob",
|
||||
Age: 30,
|
||||
}
|
||||
|
||||
// 访问结构体字段
|
||||
fmt.Println(p1.Name) // Alice
|
||||
fmt.Println(p2.Age) // 30
|
||||
|
||||
// 调用结构体方法
|
||||
p1.Greet() // Hello, my name is Alice and I'm 25 years old.
|
||||
p2.Greet() // Hello, my name is Bob and I'm 30 years old.
|
||||
|
||||
// 嵌套结构体初始化
|
||||
emp := Employee{
|
||||
Person: Person{
|
||||
Name: "Charlie",
|
||||
Age: 35,
|
||||
},
|
||||
JobTitle: "Developer",
|
||||
}
|
||||
|
||||
// 访问嵌套结构体字段
|
||||
fmt.Println(emp.Name) // Charlie (提升字段)
|
||||
fmt.Println(emp.JobTitle) // Developer
|
||||
emp.Greet() // Hello, my name is Charlie and I'm 35 years old.
|
||||
}
|
3
02_core_concepts/03_interfaces/go.mod
Normal file
3
02_core_concepts/03_interfaces/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module interfaces
|
||||
|
||||
go 1.21
|
61
02_core_concepts/03_interfaces/main.go
Normal file
61
02_core_concepts/03_interfaces/main.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 定义一个接口
|
||||
type Shape interface {
|
||||
Area() float64
|
||||
}
|
||||
|
||||
// 定义一个结构体:Rectangle
|
||||
type Rectangle struct {
|
||||
Width, Height float64
|
||||
}
|
||||
|
||||
// 实现接口方法:Area
|
||||
func (r Rectangle) Area() float64 {
|
||||
return r.Width * r.Height
|
||||
}
|
||||
|
||||
// 定义另一个结构体:Circle
|
||||
type Circle struct {
|
||||
Radius float64
|
||||
}
|
||||
|
||||
// 实现接口方法:Area
|
||||
func (c Circle) Area() float64 {
|
||||
return 3.14 * c.Radius * c.Radius
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 创建接口类型的切片
|
||||
shapes := []Shape{
|
||||
Rectangle{Width: 10, Height: 5},
|
||||
Circle{Radius: 7},
|
||||
}
|
||||
|
||||
shapes2 := []Shape{
|
||||
Rectangle{Width: 20, Height: 10},
|
||||
Circle{Radius: 14},
|
||||
Rectangle{Width: 30, Height: 15},
|
||||
Rectangle{5, 5},
|
||||
}
|
||||
|
||||
// 遍历并调用接口方法
|
||||
for _, shape := range shapes {
|
||||
fmt.Printf("Shape Area: %.2f\n", shape.Area())
|
||||
}
|
||||
|
||||
// i := 0
|
||||
// for {
|
||||
// fmt.Printf("Shape Area: %.2f\n", shapes2[i].Area())
|
||||
// i++
|
||||
// if i >= len(shapes2) {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
|
||||
for _, shape := range shapes2 {
|
||||
fmt.Printf("%.2f\n", shape.Area())
|
||||
}
|
||||
}
|
3
02_core_concepts/04_error_handling/go.mod
Normal file
3
02_core_concepts/04_error_handling/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module error_handling
|
||||
|
||||
go 1.21
|
107
02_core_concepts/04_error_handling/main.go
Normal file
107
02_core_concepts/04_error_handling/main.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors" // 提供基本错误处理功能
|
||||
"fmt" // 用于格式化输出
|
||||
"os" // 提供操作系统功能
|
||||
"strconv" // 用于字符串转换
|
||||
)
|
||||
|
||||
// DivisionError 是一个自定义错误类型
|
||||
// 它包含除法运算的被除数、除数和错误信息
|
||||
type DivisionError struct {
|
||||
dividend float64 // 被除数
|
||||
divisor float64 // 除数
|
||||
message string // 错误信息
|
||||
}
|
||||
|
||||
// Error 方法实现了 error 接口
|
||||
// 这使得 DivisionError 可以作为一个错误类型使用
|
||||
func (e *DivisionError) Error() string {
|
||||
return fmt.Sprintf("%s: %f / %f", e.message, e.dividend, e.divisor)
|
||||
}
|
||||
|
||||
// divide 函数执行除法运算,并处理除数为零的情况
|
||||
// 参数:
|
||||
// - dividend: 被除数
|
||||
// - divisor: 除数
|
||||
//
|
||||
// 返回:
|
||||
// - float64: 除法结果
|
||||
// - error: 可能的错误
|
||||
func divide(dividend, divisor float64) (float64, error) {
|
||||
if divisor == 0 {
|
||||
// 当除数为零时,返回自定义错误
|
||||
return 0, &DivisionError{
|
||||
dividend: dividend,
|
||||
divisor: divisor,
|
||||
message: "cannot divide by zero",
|
||||
}
|
||||
}
|
||||
return dividend / divisor, nil
|
||||
}
|
||||
|
||||
// readConfig 函数读取配置文件并演示错误包装
|
||||
// 使用 fmt.Errorf 和 %w 来包装底层错误
|
||||
func readConfig(filename string) (string, error) {
|
||||
data, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
// 使用 %w 包装原始错误,保留错误链
|
||||
return "", fmt.Errorf("failed to read config file: %w", err)
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// parseAge 函数解析年龄字符串,展示多重错误处理
|
||||
// 包含多个验证步骤,每步都可能产生不同的错误
|
||||
func parseAge(age string) (int, error) {
|
||||
// 检查空字符串
|
||||
if age == "" {
|
||||
return 0, errors.New("age cannot be empty")
|
||||
}
|
||||
|
||||
// 将字符串转换为整数
|
||||
n, err := strconv.Atoi(age)
|
||||
if err != nil {
|
||||
// 包装转换错误
|
||||
return 0, fmt.Errorf("invalid age format: %w", err)
|
||||
}
|
||||
|
||||
// 验证年龄范围
|
||||
if n < 0 || n > 150 {
|
||||
return 0, fmt.Errorf("age %d is out of valid range (0-150)", n)
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 示例1:演示自定义错误的使用
|
||||
result, err := divide(10, 0)
|
||||
if err != nil {
|
||||
fmt.Printf("Division error: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("Result: %f\n", result)
|
||||
}
|
||||
|
||||
// 示例2:演示错误包装和错误类型断言
|
||||
_, err = readConfig("nonexistent.txt")
|
||||
if err != nil {
|
||||
fmt.Printf("Config error: %v\n", err)
|
||||
// 使用 os.IsNotExist 判断具体的错误类型
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Println("The file does not exist")
|
||||
}
|
||||
}
|
||||
|
||||
// 示例3:演示多重错误处理
|
||||
ages := []string{"25", "-5", "abc", "200", ""}
|
||||
for _, age := range ages {
|
||||
parsedAge, err := parseAge(age)
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing age '%s': %v\n", age, err)
|
||||
continue
|
||||
}
|
||||
fmt.Printf("Valid age: %d\n", parsedAge)
|
||||
}
|
||||
}
|
3
02_core_concepts/05_concurrency/01_goroutines/go.mod
Normal file
3
02_core_concepts/05_concurrency/01_goroutines/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module goroutines
|
||||
|
||||
go 1.21
|
28
02_core_concepts/05_concurrency/01_goroutines/main.go
Normal file
28
02_core_concepts/05_concurrency/01_goroutines/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 模拟一个耗时的计算任务
|
||||
func heavyTask(id int) {
|
||||
fmt.Printf("任务 %d 开始执行\n", id)
|
||||
time.Sleep(2 * time.Second)
|
||||
fmt.Printf("任务 %d 执行完成\n", id)
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("程序开始...")
|
||||
|
||||
// 启动200个goroutine
|
||||
for i := 1; i <= 200; i++ {
|
||||
go heavyTask(i)
|
||||
}
|
||||
|
||||
// 主线程等待一段时间,确保goroutine有足够时间执行
|
||||
// 注意:在实际生产环境中应该使用更好的同步机制
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
fmt.Println("程序结束...")
|
||||
}
|
3
02_core_concepts/05_concurrency/02_channels/go.mod
Normal file
3
02_core_concepts/05_concurrency/02_channels/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module channels
|
||||
|
||||
go 1.21
|
5
02_core_concepts/05_concurrency/02_channels/main.go
Normal file
5
02_core_concepts/05_concurrency/02_channels/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
02_core_concepts/05_concurrency/03_sync_pkg/go.mod
Normal file
3
02_core_concepts/05_concurrency/03_sync_pkg/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module sync_pkg
|
||||
|
||||
go 1.21
|
5
02_core_concepts/05_concurrency/03_sync_pkg/main.go
Normal file
5
02_core_concepts/05_concurrency/03_sync_pkg/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
02_core_concepts/05_concurrency/04_more/go.mod
Normal file
3
02_core_concepts/05_concurrency/04_more/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module morecon
|
||||
|
||||
go 1.24.1
|
56
02_core_concepts/05_concurrency/04_more/main.go
Normal file
56
02_core_concepts/05_concurrency/04_more/main.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
// 判断一个数是否为素数
|
||||
func isPrime(n int) bool {
|
||||
if n <= 1 {
|
||||
return false
|
||||
}
|
||||
sqrt := int(math.Sqrt(float64(n)))
|
||||
for i := 2; i <= sqrt; i++ {
|
||||
if n%i == 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func worker(id int, jobs <-chan int, results chan<- int) {
|
||||
for job := range jobs {
|
||||
fmt.Printf("Worker %d processing job %d\n", id, job)
|
||||
// 计算从job开始的前100个数中有多少个素数
|
||||
count := 0
|
||||
for i := job; i < job+100; i++ {
|
||||
if isPrime(i) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
results <- count // 将结果发送到results通道
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
jobs := make(chan int, 100) // 工作通道
|
||||
results := make(chan int, 100) // 结果通道
|
||||
|
||||
// 启动3个工作协程
|
||||
for w := 1; w <= 3; w++ {
|
||||
go worker(w, jobs, results)
|
||||
}
|
||||
|
||||
// 发送工作任务
|
||||
for j := 1; j <= 80; j++ {
|
||||
jobs <- j
|
||||
}
|
||||
close(jobs)
|
||||
|
||||
// 收集结果
|
||||
for i := 1; i <= 80; i++ {
|
||||
result := <-results
|
||||
fmt.Printf("Got result: %d\n", result) // 输出每个范围内的素数个数
|
||||
}
|
||||
}
|
3
03_std_lib/01_fmt/go.mod
Normal file
3
03_std_lib/01_fmt/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module fmt
|
||||
|
||||
go 1.23.5
|
BIN
03_std_lib/01_fmt/main.go
Normal file
BIN
03_std_lib/01_fmt/main.go
Normal file
Binary file not shown.
3
03_std_lib/02_os/go.mod
Normal file
3
03_std_lib/02_os/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module os
|
||||
|
||||
go 1.21
|
5
03_std_lib/02_os/main.go
Normal file
5
03_std_lib/02_os/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
03_std_lib/03_io/go.mod
Normal file
3
03_std_lib/03_io/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module io
|
||||
|
||||
go 1.21
|
5
03_std_lib/03_io/main.go
Normal file
5
03_std_lib/03_io/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
03_std_lib/04_net_http/go.mod
Normal file
3
03_std_lib/04_net_http/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module net_http
|
||||
|
||||
go 1.21
|
5
03_std_lib/04_net_http/main.go
Normal file
5
03_std_lib/04_net_http/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
03_std_lib/05_encoding_json/go.mod
Normal file
3
03_std_lib/05_encoding_json/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module encoding_json
|
||||
|
||||
go 1.21
|
5
03_std_lib/05_encoding_json/main.go
Normal file
5
03_std_lib/05_encoding_json/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
03_std_lib/06_testing/go.mod
Normal file
3
03_std_lib/06_testing/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module testing
|
||||
|
||||
go 1.21
|
5
03_std_lib/06_testing/main.go
Normal file
5
03_std_lib/06_testing/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
04_tools/01_go_mod/go.mod
Normal file
3
04_tools/01_go_mod/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module go_mod
|
||||
|
||||
go 1.21
|
5
04_tools/01_go_mod/main.go
Normal file
5
04_tools/01_go_mod/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
04_tools/02_go_test/go.mod
Normal file
3
04_tools/02_go_test/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module go_test
|
||||
|
||||
go 1.21
|
5
04_tools/02_go_test/main.go
Normal file
5
04_tools/02_go_test/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
04_tools/03_go_vet/go.mod
Normal file
3
04_tools/03_go_vet/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module go_vet
|
||||
|
||||
go 1.21
|
5
04_tools/03_go_vet/main.go
Normal file
5
04_tools/03_go_vet/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
04_tools/04_go_fmt/go.mod
Normal file
3
04_tools/04_go_fmt/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module go_fmt
|
||||
|
||||
go 1.21
|
5
04_tools/04_go_fmt/main.go
Normal file
5
04_tools/04_go_fmt/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
04_tools/05_debug_tools/go.mod
Normal file
3
04_tools/05_debug_tools/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module debug_tools
|
||||
|
||||
go 1.21
|
5
04_tools/05_debug_tools/main.go
Normal file
5
04_tools/05_debug_tools/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
05_advanced/01_reflection/go.mod
Normal file
3
05_advanced/01_reflection/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module reflection
|
||||
|
||||
go 1.21
|
5
05_advanced/01_reflection/main.go
Normal file
5
05_advanced/01_reflection/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
05_advanced/02_performance/go.mod
Normal file
3
05_advanced/02_performance/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module performance
|
||||
|
||||
go 1.21
|
5
05_advanced/02_performance/main.go
Normal file
5
05_advanced/02_performance/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
05_advanced/03_memory_management/go.mod
Normal file
3
05_advanced/03_memory_management/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module memory_management
|
||||
|
||||
go 1.21
|
5
05_advanced/03_memory_management/main.go
Normal file
5
05_advanced/03_memory_management/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
05_advanced/04_network_programming/go.mod
Normal file
3
05_advanced/04_network_programming/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module network_programming
|
||||
|
||||
go 1.21
|
5
05_advanced/04_network_programming/main.go
Normal file
5
05_advanced/04_network_programming/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
05_advanced/05_database/go.mod
Normal file
3
05_advanced/05_database/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module database
|
||||
|
||||
go 1.21
|
5
05_advanced/05_database/main.go
Normal file
5
05_advanced/05_database/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
06_projects/01_cli_tool/go.mod
Normal file
3
06_projects/01_cli_tool/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module cli_tool
|
||||
|
||||
go 1.21
|
5
06_projects/01_cli_tool/main.go
Normal file
5
06_projects/01_cli_tool/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
06_projects/02_web_service/go.mod
Normal file
3
06_projects/02_web_service/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module web_service
|
||||
|
||||
go 1.21
|
5
06_projects/02_web_service/main.go
Normal file
5
06_projects/02_web_service/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
06_projects/03_crawler/go.mod
Normal file
3
06_projects/03_crawler/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module crawler
|
||||
|
||||
go 1.21
|
5
06_projects/03_crawler/main.go
Normal file
5
06_projects/03_crawler/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
06_projects/04_microservice/go.mod
Normal file
3
06_projects/04_microservice/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module microservice
|
||||
|
||||
go 1.21
|
5
06_projects/04_microservice/main.go
Normal file
5
06_projects/04_microservice/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
3
06_projects/05_distributed_system/go.mod
Normal file
3
06_projects/05_distributed_system/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module distributed_system
|
||||
|
||||
go 1.21
|
5
06_projects/05_distributed_system/main.go
Normal file
5
06_projects/05_distributed_system/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// TODO
|
||||
}
|
53
go_learning_roadmap.md
Normal file
53
go_learning_roadmap.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# Go语言学习路线图
|
||||
|
||||
## 基础语法
|
||||
- 变量与常量
|
||||
- 数据类型
|
||||
- 运算符
|
||||
- 流程控制
|
||||
- 条件语句
|
||||
- 循环语句
|
||||
- 函数
|
||||
- 定义与调用
|
||||
- 多返回值
|
||||
- 匿名函数
|
||||
- 包管理
|
||||
|
||||
## 核心概念
|
||||
- 指针
|
||||
- 结构体
|
||||
- 接口
|
||||
- 错误处理
|
||||
- 并发编程
|
||||
- Goroutine
|
||||
- Channel
|
||||
- sync包
|
||||
|
||||
## 标准库
|
||||
- fmt
|
||||
- os
|
||||
- io
|
||||
- net/http
|
||||
- encoding/json
|
||||
- testing
|
||||
|
||||
## 工具链
|
||||
- go mod
|
||||
- go test
|
||||
- go vet
|
||||
- go fmt
|
||||
- 调试工具
|
||||
|
||||
## 进阶主题
|
||||
- 反射
|
||||
- 性能优化
|
||||
- 内存管理
|
||||
- 网络编程
|
||||
- 数据库操作
|
||||
|
||||
## 实战项目
|
||||
1. 命令行工具
|
||||
2. Web服务
|
||||
3. 爬虫程序
|
||||
4. 微服务
|
||||
5. 分布式系统
|
Reference in New Issue
Block a user