This commit is contained in:
2024-08-01 23:40:06 +08:00
commit 1adca2ffe6
17 changed files with 469 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.idea/
test

44
demo/dem.go Normal file
View File

@@ -0,0 +1,44 @@
package main //同文件包的声明 需要保持一直
import (
"fmt"
"strconv"
"strings"
)
func main() {
var s string = "golang你好"
fmt.Println(len(s))
for i, value := range s {
fmt.Printf("%d", "%c", i, value)
}
r := []rune(s)
for i := 0; i < len(r); i++ {
fmt.Println(r[i])
}
num1, _ := strconv.Atoi("666")
fmt.Println(num1)
//整数转成字符串
st1 := strconv.Itoa(88)
fmt.Println(st1)
//统计字符串中有几个相同的
count := strings.Count("golanggolang", "golang")
fmt.Println(count)
//不区分大小写
flag := strings.EqualFold("go", "GO")
fmt.Println(flag)
//区分大小写
fmt.Println("hello" == "HELLO")
//字符串替换
ss := strings.Replace("gogogo", "go", "golang", -1)
fmt.Println(ss)
//按照字符串,为分割符标识,将字符串进行切割分成字符数组
a := strings.Split("java-go", "-")
fmt.Println(a)
//大小写切换
fmt.Println(strings.ToLower("GO"))
fmt.Println(strings.ToUpper("go"))
//去除空格!
fmt.Println(strings.TrimSpace(" go "))
}

26
demo/demo01.go Normal file
View File

@@ -0,0 +1,26 @@
package main
import (
"fmt"
)
func main() {
var a int = 100
var b int = 30
c := sum(a, b)
fmt.Println(c)
test(32, 54)
}
// 函数的首字母大写可以被其他包的文件访问(类似于public)
// 函数的首字母小写只能被本包访问(类似于private)
func sum(a int, b int) int {
return a + b
}
func test(args ...int) {
for i := 0; i < len(args); i++ {
fmt.Println(args[i])
}
}

37
demo/demo3.go Normal file
View File

@@ -0,0 +1,37 @@
package main
import "fmt"
type Animal struct {
Age int
Weight float32
}
func (an *Animal) shou() {
fmt.Println("打击")
}
func (an *Animal) showinfo() {
fmt.Println("动物的体重", an.Age, an.Weight)
}
type Cat struct {
Animal
Age int
}
func (c *Cat) showInfo() {
fmt.Println("动物体重", c.Age, c.Weight)
}
// 对cat绑定特有的方法
func (c *Cat) rs() {
fmt.Println("我是小猫")
}
func main() {
cat := &Cat{}
cat.Weight = 10
cat.Animal.Age = 30
fmt.Println(cat)
}

26
demo/p2.go Normal file
View File

@@ -0,0 +1,26 @@
package main
import "fmt"
type person struct {
Name string
age int
}
// 定义工厂的函数
func NewPerson(name string) *person {
return &person{
Name: name,
}
}
func (p *person) SetAge(age int) {
if age >= 0 && age <= 150 {
p.age = age
} else {
fmt.Println("超出")
}
}
func (p *person) GetAge() int {
return p.age
}

5
demo/te.go Normal file
View File

@@ -0,0 +1,5 @@
package main
func main() {
}

52
demo/test.go Normal file
View File

@@ -0,0 +1,52 @@
package main
import "fmt"
// 接口定义设计规范
type SayHello interface {
SayHello()
}
type American struct {
Name string
}
func (person American) SayHello() {
fmt.Println("你好")
}
type chinese struct {
Name string
}
func (person chinese) SayHello() {
fmt.Println("chinese")
}
func (person chinese) tiaowu() {
fmt.Println("跳舞")
}
// 定义函数:接收接口的实例
func greet(s SayHello) {
s.SayHello()
//断言控制语句的要求 判断这是chinese
ch, flag := s.(chinese)
if flag == true {
ch.tiaowu()
} else {
fmt.Println("no")
}
}
func main() {
var a [3]SayHello
a[0] = chinese{"1"}
a[1] = American{"hel"}
a[0].SayHello()
c := chinese{}
c.tiaowu()
c.SayHello()
}

26
demo/test01.go Normal file
View File

@@ -0,0 +1,26 @@
package main
import "fmt"
type a interface {
}
type b interface {
}
type Stu struct {
}
func (s Stu) a() {
fmt.Println("aa")
}
func (s Stu) b() {
fmt.Println("bbb")
}
func main() {
var s Stu
var a = s
var b = s
a.b()
b.a()
}

26
main.go Normal file
View File

@@ -0,0 +1,26 @@
package main
import "fmt"
func GetSum() func(int) int {
var sum int = 0
return func(x int) int {
sum = sum + x
return sum
}
}
func add(num1 int, num2 int) int {
defer fmt.Println(num1) //3
defer fmt.Println(num2) //进入栈中 栈的特点是 先进后出2
var sum int = num1 + num2
fmt.Println(sum) //先执行1
return sum //4
}
func main() {
f := GetSum()
fmt.Println(f(1))
fmt.Println(f(2))
fmt.Println(f(3))
fmt.Println(add(30, 60))
}

45
main/defers.go Normal file
View File

@@ -0,0 +1,45 @@
package main
import (
"errors"
"fmt"
)
func t() error {
a := 10
b := 5
if b == 0 {
return errors.New("除数不能为零")
} else {
res := a / b
fmt.Println(res)
return nil
}
}
func main() {
err := t()
if err != nil {
fmt.Println("自定义错误")
//停止后面程序继续执行
panic(err)
}
fmt.Println("继续执行")
var scores [5]int
scores[0] = 10
scores[1] = 10
scores[2] = 30
scores[3] = 40
for i := 0; i < len(scores); i++ {
fmt.Println(scores[i])
fmt.Scanln(&scores[i])
}
//索引用key接收 value用于里面的值
for key, value := range scores {
fmt.Println(key, value)
}
}

20
main/der.go Normal file
View File

@@ -0,0 +1,20 @@
package main
import "fmt"
func main() {
var book Book
book.auth = "1u1"
book.name = "aihda"
changebook(&book)
fmt.Println(book)
}
func changebook(book *Book) {
book.auth = "77"
}
type Book struct {
name string
auth string
}

34
main/p1.go Normal file
View File

@@ -0,0 +1,34 @@
package main
import "fmt"
type Student struct {
Name string
}
// 函数
func mthod(s Student) {
fmt.Println(s.Name)
}
func mthod1(s *Student) {
fmt.Println((*s).Name)
}
func (s *Student) test02() {
fmt.Println(s.Name)
}
func main() {
var stu = Student{"笑傲"}
mthod(stu)
mthod1(&stu)
(&stu).test02()
var s = Student{
Name: "xio",
}
fmt.Println(s)
var s3 *student = &student{
age: 19,
}
fmt.Println(s3)
}

26
main/pg.go Normal file
View File

@@ -0,0 +1,26 @@
package main
import "fmt"
func main() {
var a [7]int = [7]int{4, 5, 6, 6, 4, 7, 7}
var slice []int = a[2:4]
fmt.Println(len(slice))
slice2 := append(slice, 55, 55)
fmt.Println(slice2)
fmt.Println(len(slice2))
//默认分配10个空间
var ip map[int]string
//key不能重复,前面会替换后面那个 value 可以重复
ip = make(map[int]string, 10)
ip[103883] = "张三"
ip[173718] = "李四"
ip[173718] = "李"
fmt.Println(ip)
//查找
value, flag := ip[103883]
fmt.Println(value)
fmt.Println(flag)
fmt.Println(len(ip))
}

22
main/pp.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import "fmt"
func main() {
tes()
}
func tes() int {
//利用defer+recover来捕获错误defer加匿名函数调用
defer func() {
err := recover()
if err != nil {
fmt.Println("错误已经捕获")
fmt.Println(err)
}
}()
num02 := 10
num01 := 0
return num02 / num01
}

22
main/project.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import "fmt" //导入别的包 先执行别的包里面 再执行自己包里面
//做变量初始化
// 后是 init函数 全局变量再执行main函数调用
var num int = test()
func test() int {
fmt.Println("test")
return 10
}
func init() {
fmt.Println("1")
}
func main() {
fmt.Println("2")
}

35
main/pt.go Normal file
View File

@@ -0,0 +1,35 @@
package main
import (
"fmt"
)
func main() {
//创建对象实例
var T teacher = teacher{}
T.Name = "dakjda"
fmt.Println(T)
//使用指针创建对象
var t *teacher = new(teacher)
(*t).school = "没用"
(*t).age = 19
fmt.Println(t)
var s student
var p person
s = student(p)
fmt.Println(s)
}
// 定义结构体 方便内存管理
type teacher struct {
Name string
age int64
school string
}
type student struct {
age int
}
type person struct {
age int
}

21
main/pu.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"fmt"
)
func main() {
//定义切片make函数的三个参数1.切片类型。2.切片的长度
slice := make([]int, 4, 20)
fmt.Println(slice)
fmt.Println(len(slice))
fmt.Println(cap(slice))
slice[0] = 66
slice[1] = 88
slice1 := []int{3, 4, 5}
fmt.Println(slice1)
//按照数组的方式操作
for key, value := range slice1 {
fmt.Println(key, value)
}
}