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

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()
}