diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0f06797 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "stdio.h": "c" + } +} \ No newline at end of file diff --git a/Go入门速成/Day1/Class1 安装、初始化Go.md b/Go入门速成/Day1/Class1 安装、初始化Go.md index fe9f0d8..b4ed52e 100644 --- a/Go入门速成/Day1/Class1 安装、初始化Go.md +++ b/Go入门速成/Day1/Class1 安装、初始化Go.md @@ -32,10 +32,9 @@ func main() { } ``` - 我们写完了程序,如何把这个程序运行起来呢,分为两步: - - 第一步:执行指令`go mod init`,初始化与版本相关联的 Go 包的集合,确定了根目录、定义了项目的依赖和版本,确保项目可以重建。(也叫做Go的模块)!这一步,会在当前路径下创建`go.mod`文件 + - 第一步:执行指令`go mod init xxx` ,初始化与版本相关联的 Go 包的集合,确定了根目录、定义了项目的依赖和版本,确保项目可以重建。(也叫做Go的模块)!这一步,会在当前路径下创建`go.mod`文件 - 第二步:执行指令`go mod tidy`,拉取我们需要的go的组建(又叫做库),这个操作可以类比为`pip install -r requirements.txt`,所需要的包IDE会自动写入`go.mod`文件中。 - ![image.png](https://krust.top:5244/d/public/KrustBlogPNG/20250319090617163.png?sign=j3448Wo2gLjatAdHKplY3LSbHQv7-H445FLMbOhOFk0=:0) - 随后,通过上面这两步,go程序就初始化好了,相关的包也下载好了,接下来,我们就能重新运行了 - - 运行项目:`go run` 开始执行整个项目 - - 运行单个程序: `go run xxx.go` + - 运行项目: `go run xxx.go` - 构建go项目(将整个项目打包成为可执行文件) `go build` \ No newline at end of file diff --git a/Go入门速成/Day1/Class2 Go的基本语法.md b/Go入门速成/Day1/Class2 Go的基本语法.md index e69de29..f030795 100644 --- a/Go入门速成/Day1/Class2 Go的基本语法.md +++ b/Go入门速成/Day1/Class2 Go的基本语法.md @@ -0,0 +1,265 @@ +### **1. 基础结构** +```go +package main // 包声明(必须) +import "fmt" // 导入包 + +func main() { // 主函数(程序入口) + fmt.Println("Hello, World!") +} +``` + +--- + +### **2. 变量与常量** +- **变量声明** + ```go + var a int = 10 // 显式类型 + var b = 20 // 类型推断 + c := 30 // 短声明(函数内使用) + var d, e int = 1, 2 // 多变量声明 + ``` + +- **常量** + ```go + const Pi = 3.14 + const ( + A = 1 + B = 2 + ) + ``` +- 着重强调:**开头是大写的是Public!** +--- + +### **3. 基本数据类型** +- **基本类型** + ```go + int, int8, int16, int32, int64 + uint, uint8, uint16, uint32, uint64 + float32, float64 + bool + string + byte (等同于 uint8) + rune (等同于 int32, 表示 Unicode 字符) + ``` + +- **复合类型** + ```go + var arr [3]int // 数组(固定长度) + slice := []int{1, 2, 3} // 切片(动态数组) + m := map[string]int{"key": 1} // 映射(字典) + type Person struct { Name string } // 结构体 + ``` + +--- + +### **4. 控制结构** +- **条件语句** + ```go + if x > 0 { + // ... + } else if x == 0 { + // ... + } else { + // ... + } + ``` + +- **循环** + ```go + for i := 0; i < 10; i++ { ... } // 传统 for 循环 + for i < 10 { ... } // 类似 while + for index, value := range slice { ... } // 遍历切片/数组/map + ``` + +- **Switch** + ```go + switch x { + case 1: + // ... + case 2, 3: // 多值匹配 + // ... + default: + // ... + } + ``` +- 特点:不需要break! +--- + +### **5. 函数** +- **基本函数** + ```go + func add(a int, b int) int { + return a + b + } + ``` + +- **多返回值** + ```go + func swap(a, b int) (int, int) { + return b, a + } + ``` + +- **命名返回值** + ```go + func split(sum int) (x, y int) { + x = sum * 4 / 9 + y = sum - x + return // 隐式返回 x, y + } + ``` + +- **匿名函数与闭包**(即函数内套函数) + ```go + func() { + fmt.Println("Anonymous function") + }() + ``` + +--- + +### **6. 指针与结构体** +- **指针** + ```go + var p *int + x := 10 + p = &x + *p = 20 // 修改 x 的值 + ``` +- 特点: + - Go 的指针**不支持算术运算**,避免了内存越界和非法访问的风险,同时通过垃圾回收机制自动管理内存,**减少了内存泄漏的可能性**。 + - Go 的指针类型严格区分,空指针用 `nil` 表示,解引用空指针会触发 panic,不支持**指针算术运算和强制类型转换**。 +- **结构体与方法** + ```go + type Circle struct { + Radius float64 + } + + // 方法(值接收者) + func (c Circle) Area() float64 { + return math.Pi * c.Radius * c.Radius + } + + // 方法(指针接收者) + func (c *Circle) Scale(factor float64) { + c.Radius *= factor + } + ``` + +--- + +### **7. 接口与错误处理** +- **接口** + ```go + type Shape interface { + Area() float64 + } + + // 隐式实现接口 + func (c Circle) Area() float64 { ... } + ``` + +- **错误处理** + ```go + func readFile() ([]byte, error) { + data, err := os.ReadFile("file.txt") + if err != nil { + return nil, err + } + return data, nil + } + + // 调用 + data, err := readFile() + if err != nil { + log.Fatal(err) + } + ``` + +- **Panic & Recover** + ```go + func safeCall() { + defer func() { + if r := recover(); r != nil { + fmt.Println("Recovered:", r) + } + }() + panic("Something went wrong!") + } + ``` + +--- + +### **8. 并发编程** +- **Goroutine** + ```go + go func() { + fmt.Println("Running in goroutine") + }() + ``` + +- **Channel**(数据通道) + ```go + ch := make(chan int) + go func() { ch <- 1 }() // 发送数据 + value := <-ch // 接收数据 + ``` + +- **Select**(主要用于事件驱动) + ```go + select { + case msg1 := <-ch1: + fmt.Println(msg1) + case msg2 := <-ch2: + fmt.Println(msg2) + case <-time.After(1 * time.Second): + fmt.Println("Timeout") + } + ``` + +--- + +### **9. 包与模块** +- **创建模块** + ```bash + go mod init xx + ``` + +- **导入包** + ```go + import ( + "fmt" + "math/rand" + "github.com/user/package" + ) + ``` + +--- + +### **10. 其他特性** +- **Defer** +- `defer` 是 Go 语言中的一个关键字,用于延迟执行一个函数调用。**被 `defer` 修饰的函数调用会推迟到当前函数返回之前执行**,无论当前函数是正常返回还是由于错误(如 `panic`)提前返回。`defer` 的主要用途是确保某些操作(如资源释放、清理工作等)一定会被执行,避免遗漏。 + ```go + func readFile() { + file, _ := os.Open("file.txt") + defer file.Close() // 函数返回前执行 + // ... + } + ``` + +- **JSON 处理** + ```go + type User struct { + Name string `json:"name"` + Age int `json:"age"` + } + data, _ := json.Marshal(user) //序列化 + ``` + +--- + +### **常用内置函数** +- `len()`:获取长度 +- `cap()`:切片容量 +- `make()`:创建切片/map/channel +- `append()`:切片追加元素 diff --git a/Go入门速成/Day1/ExampleCode/BasicGrammar/BasicGrammar b/Go入门速成/Day1/ExampleCode/BasicGrammar/BasicGrammar new file mode 100755 index 0000000..a3bd77e Binary files /dev/null and b/Go入门速成/Day1/ExampleCode/BasicGrammar/BasicGrammar differ diff --git a/Go入门速成/Day1/ExampleCode/BasicGrammar/PointerPanic.go b/Go入门速成/Day1/ExampleCode/BasicGrammar/PointerPanic.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/Go入门速成/Day1/ExampleCode/BasicGrammar/PointerPanic.go @@ -0,0 +1 @@ +package main diff --git a/Go入门速成/Day1/ExampleCode/BasicGrammar/go.mod b/Go入门速成/Day1/ExampleCode/BasicGrammar/go.mod new file mode 100644 index 0000000..110fe6f --- /dev/null +++ b/Go入门速成/Day1/ExampleCode/BasicGrammar/go.mod @@ -0,0 +1,3 @@ +module BasicGrammar + +go 1.24.1 diff --git a/Go入门速成/Day1/ExampleCode/BasicGrammar/main.go b/Go入门速成/Day1/ExampleCode/BasicGrammar/main.go new file mode 100644 index 0000000..d69aa4c --- /dev/null +++ b/Go入门速成/Day1/ExampleCode/BasicGrammar/main.go @@ -0,0 +1,147 @@ +package main + +import ( + "errors" + "fmt" + "sync" + "time" +) + +// 常量 +const Pi = 3.14159 + +// 结构体 +type Circle struct { + Radius float64 +} + +// 方法(值接收者) +func (c Circle) Area() float64 { + return Pi * c.Radius * c.Radius +} + +// 方法(指针接收者) +func (c *Circle) Scale(factor float64) { + c.Radius *= factor +} + +// 接口 +type Shape interface { + Area() float64 +} + +// 函数(多返回值) +func divide(a, b float64) (float64, error) { + if b == 0 { + return 0, errors.New("division by zero") + } + return a / b, nil +} + +// 主函数 +func main() { + // 变量声明 + var a int = 10 + b := 20 + c, d := 30, 40 + fmt.Println("Variables:", a, b, c, d) + + // 数组与切片 + arr := [3]int{1, 2, 3} + slice := []int{4, 5, 6} + slice = append(slice, 7) + fmt.Println("Array:", arr, "Slice:", slice) + + // 映射 + m := map[string]int{"one": 1, "two": 2} + fmt.Println("Map:", m) + + // 控制结构 + if a > 5 { + fmt.Println("a is greater than 5") + } else { + fmt.Println("a is not greater than 5") + } + + for i := 0; i < 3; i++ { + fmt.Println("Loop:", i) + } + + switch a { + case 10: + fmt.Println("a is 10") + default: + fmt.Println("a is not 10") + } + + // 函数调用 + result, err := divide(10, 2) + if err != nil { + fmt.Println("Error:", err) + } else { + fmt.Println("Division result:", result) + } + + // 指针 + x := 10 + p := &x + *p = 20 + fmt.Println("Pointer:", x) + + // 结构体与方法 + circle := Circle{Radius: 5} + fmt.Println("Circle area:", circle.Area()) + circle.Scale(2) + fmt.Println("Scaled circle area:", circle.Area()) + + // 接口 + var shape Shape = Circle{Radius: 3} + fmt.Println("Shape area:", shape.Area()) + + // 错误处理 + _, err = divide(10, 0) + if err != nil { + fmt.Println("Error:", err) + } + + // 并发 + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + fmt.Println("Running in goroutine") + }() + wg.Wait() + + // Channel + ch := make(chan int) + go func() { + ch <- 42 + }() + value := <-ch + fmt.Println("Channel value:", value) + + // Select + ch1 := make(chan string) + ch2 := make(chan string) + go func() { + time.Sleep(1 * time.Second) + ch1 <- "from ch1" + }() + go func() { + time.Sleep(2 * time.Second) + ch2 <- "from ch2" + }() + select { + case msg := <-ch1: + fmt.Println("Select:", msg) + case msg := <-ch2: + fmt.Println("Select:", msg) + case <-time.After(3 * time.Second): + fmt.Println("Timeout") + } + + // Defer + defer fmt.Println("Defer: This runs last") + fmt.Println("Main function end") +} diff --git a/Go入门速成/Day1/ExampleCode/BasicGrammar/switch.c b/Go入门速成/Day1/ExampleCode/BasicGrammar/switch.c new file mode 100644 index 0000000..734c21b --- /dev/null +++ b/Go入门速成/Day1/ExampleCode/BasicGrammar/switch.c @@ -0,0 +1,21 @@ +//go:build ignore +// +build ignore + + +#include "stdio.h" + +int main() { + int a = 2; + switch (a) { + case 1: + printf("a = 1\n"); + break; + case 2: + printf("a = 2\n"); + break; + case 3: + printf("a = 3\n"); + break; + default: + printf("FUCK"); // default 语句是可选的 +} \ No newline at end of file diff --git a/Go入门速成/Day1/ExampleCode/Helloworld/main b/Go入门速成/Day1/ExampleCode/Helloworld/main deleted file mode 100755 index 6ce6e10..0000000 Binary files a/Go入门速成/Day1/ExampleCode/Helloworld/main and /dev/null differ diff --git a/Go入门速成/Day2/Class3 Gorm的使用.md b/Go入门速成/Day2/Class3 Gorm的使用.md new file mode 100644 index 0000000..86c5bdf --- /dev/null +++ b/Go入门速成/Day2/Class3 Gorm的使用.md @@ -0,0 +1,31 @@ +# Gorm的使用 +## Gorm是什么 +- GORM 是 Go 语言的 ORM 库,提供模型定义、关联管理、事务支持、查询构建、数据迁移、钩子回调等功能,支持主流数据库(如 MySQL/PostgreSQL/SQLite),简化数据库操作。 +## 如何使用Gorm +### 导入Gorm库 +```go +import ( +"gorm.io/driver/mysql" +) +``` +### 基本操作 +- 1、连接数据库 +```go +const ( +USERNAME = "root" +PASSWD = "oppofindx2" +DATABASENAME = "Class" +) +dsn := fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/%s?charset=utf8mb4&parseTime=True&loc=Local", USERNAME, PASSWD, DATABASENAME) +db := mysql.Open(dsn) +````` +- 2、数据库中表的定义 + - 在Gorm中,定义一张表使用的是结构体 + ```go +type User struct { + gorm.Model + Name string + Age int +} + ``` +- 自动迁移表结构(方便我们修改表,给表添加参数) \ No newline at end of file diff --git a/Go入门速成/Day2/ExampleCode/Gorm/go.mod b/Go入门速成/Day2/ExampleCode/Gorm/go.mod new file mode 100644 index 0000000..ab8ee47 --- /dev/null +++ b/Go入门速成/Day2/ExampleCode/Gorm/go.mod @@ -0,0 +1,12 @@ +module Gorm + +go 1.24.1 + +require gorm.io/driver/mysql v1.5.7 + +require ( + github.com/go-sql-driver/mysql v1.7.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + gorm.io/gorm v1.25.7 // indirect +) diff --git a/Go入门速成/Day2/ExampleCode/Gorm/go.sum b/Go入门速成/Day2/ExampleCode/Gorm/go.sum new file mode 100644 index 0000000..f0385d3 --- /dev/null +++ b/Go入门速成/Day2/ExampleCode/Gorm/go.sum @@ -0,0 +1,10 @@ +github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo= +gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= +gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A= +gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= diff --git a/Go入门速成/Day2/ExampleCode/Gorm/main.go b/Go入门速成/Day2/ExampleCode/Gorm/main.go new file mode 100644 index 0000000..8280399 --- /dev/null +++ b/Go入门速成/Day2/ExampleCode/Gorm/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +const ( + USERNAME = "root" + PASSWD = "oppofindx2" + DATABASENAME = "Class" +) + +func main() { + ls + dsn := fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/%s?charset=utf8mb4&parseTime=True&loc=Local", USERNAME, PASSWD, DATABASENAME) + db := mysql.Open(dsn) + print(db) +} + +type User struct { + gorm.Model + Name string + Age int +}