master
ZGGSONG 3 years ago
parent e38ee114cb
commit 6e253612ea

@ -0,0 +1,23 @@
package main
/**
0 0
*/
func main() {
}
//type ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
//func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
// return nil
//}

@ -0,0 +1,40 @@
package main
import "fmt"
/**
newmake:
1.
2. makeslicemapchannel
3. new
*/
func main() {
a := 10
b := &a
fmt.Printf("%v, %p\n", a, b)
fmt.Printf("a: %T, b: %T\n", a, b)
fmt.Printf("%v\n", *b)
modify1(a)
fmt.Printf("%v\n", a)
modify2(&a)
fmt.Printf("%v\n", a)
//Go中 引用类型的比变量不仅不要声明,还需要对其分配内存空间,否则将无法存储
//而值类型则不需要分配内存空间
//var m *int //未分配内存空间
m := new(int)
*m = 150
fmt.Println(*m)
//var mm map[string]int//为分配内存空间
mm := make(map[string]int)
mm["123"] = 1233
fmt.Println(mm)
}
func modify1(num int) {
num = 20
}
func modify2(num *int) {
*num = 100
}

@ -0,0 +1,24 @@
package main
import "fmt"
type student struct {
name string
age int
}
func main() {
m := make(map[string]*student)
stus := []student{
{name: "小王子", age: 18},
{name: "娜扎", age: 23},
{name: "大王八", age: 9000},
}
for _, stu := range stus {
m[stu.name] = &stu
}
for k, v := range m {
fmt.Println(k, "=>", v.name)
}
}

@ -0,0 +1,64 @@
package main
import (
"bufio"
"fmt"
"os"
)
/**
使
id
*/
type ExStudent struct {
ID, Age, Score, Name string
}
func main() {
var str string
exStuArr := make([]ExStudent, 0, 20)
in := bufio.NewScanner(os.Stdin)
for true {
fmt.Println("=== 学生管理系统 ===")
fmt.Println("* 1. 展示学生 *")
fmt.Println("* 2. 添加学生 *")
fmt.Println("* 3. 编辑学生 *")
fmt.Println("* 4. 删除学生 *")
fmt.Println("====================")
fmt.Println("请输入编号:")
if in.Scan() {
str = in.Text()
}
switch str {
case "1":
for _, v := range exStuArr {
fmt.Printf("ID: %v\nName: %v\nAge: %v\nScore: %v\n", v.ID, v.Name, v.Age, v.Score)
}
break
case "2":
exStu := ExStudent{}
fmt.Println("请输入ID:")
if in.Scan() {
exStu.ID = in.Text()
}
fmt.Println("请输入Age:")
if in.Scan() {
exStu.Age = in.Text()
}
fmt.Println("请输入Name:")
if in.Scan() {
exStu.Name = in.Text()
}
fmt.Println("请输入Score:")
if in.Scan() {
exStu.Score = in.Text()
}
exStuArr = append(exStuArr, exStu)
for _, v := range exStuArr {
fmt.Printf("ID: %v\nName: %v\nAge: %v\nScore: %v\n", v.ID, v.Name, v.Age, v.Score)
}
}
}
}

@ -0,0 +1,51 @@
package main
import (
"encoding/json"
"fmt"
)
type Student struct {
ID int
Gender string
Name string
}
type Class struct {
Title string
Student []*Student
}
func main() {
c := &Class{
Title: "101",
Student: make([]*Student, 0, 200),
}
for i := 0; i < 10; i++ {
stu := &Student{
ID: i,
Gender: "男",
Name: fmt.Sprintf("stu%02d", i),
}
c.Student = append(c.Student, stu)
}
//for _, v := range c.Student {
// fmt.Printf("%#v\n", v)
//}
data, err := json.Marshal(c)
if err != nil {
fmt.Println("json marshal failed")
return
}
fmt.Printf("JSON:\n %s\n", data)
str := `{"Title":"101","Student":[{"ID":0,"Gender":"男","Name":"stu00"},{"ID":1,"Gender":"男","Name":"stu01"}]}`
c1 := &Class{}
err = json.Unmarshal([]byte(str), c1)
if err != nil {
fmt.Println("json unmarshal failed")
return
}
for _, v := range c1.Student {
fmt.Printf("%#v\n", v)
}
}

@ -0,0 +1,20 @@
package main
import "fmt"
/*
struct
*/
// NewInt 类型定义
type NewInt int
// MyInt 类型别名
type MyInt = int
func main() {
var a NewInt
var b MyInt
fmt.Printf("a: %T, b: %T\n", a, b)
}

@ -0,0 +1,41 @@
package main
import "fmt"
// Person
/**
GoMethod
Receiver
this self
*/
type Person struct {
name string
age int
}
func newPerson2(name string, age int) *Person {
return &Person{
name: name,
age: age,
}
}
// Dream 方法
func (p Person) Dream() {
fmt.Printf("%s 好好学习\n", p.name)
}
//指针类型接收者 =>
//1.因为结构体是值类型,当需要修改接收者中的值的时候,需要使用指针类型的接收者
//2.接收者是拷贝代价比较大的大对象
//3.保证一致性,如果有某个方法使用了指针接收者,那么其他的方法也应该使用指针接收者。
func (p *Person) setAge(nAge int) {
p.age = nAge
}
func main() {
p1 := newPerson2("song", 18)
p1.Dream()
p1.setAge(20)
fmt.Printf("p1: %v", p1.age)
}

@ -0,0 +1,50 @@
package main
import "fmt"
type person struct {
city, name string
age int
}
func main() {
//基本实例化
var p1 person //没有初始化的结构体,其成员变量都是对应其类型的零值。
p1.age = 10
p1.city = "anhui"
p1.name = "ld"
//匿名结构体
var user struct {
name string
age int
}
user.age = 10
user.name = "username"
fmt.Println(p1, user)
//创建指针类型结构体
var p2 = new(person)
fmt.Printf("p2: %T\n", p2)
fmt.Printf("p2: %#v\n", p2)
//取结构体地址的实例化
p3 := &person{}
fmt.Printf("p3: %T\n", p3)
fmt.Printf("p3: %#v\n", p3)
p3.age = 18 //本质在底层是执行的 (*p3).age = 18
p3.city = "beijing"
p3.name = "zhang san"
fmt.Printf("p3: %#v\n", p3)
p4 := newPerson("p4", "suzhou", 22)
fmt.Printf("p4: %#v\n", p4)
}
func newPerson(name, city string, age int) *person {
return &person{
city: city,
name: name,
age: age,
}
}

@ -0,0 +1,36 @@
package main
import "fmt"
// Person2 匿名结构体
type Person2 struct {
string
int
}
// Address 嵌套结构体
type Address struct {
Province, City string
}
type User struct {
Name string
Age int
Address
}
func main() {
p1 := Person2{
"132",
18,
}
fmt.Printf("p1: %#v\n", p1)
user := User{
Name: "song",
Age: 18,
Address: Address{
Province: "anhui",
City: "An qing",
},
}
fmt.Printf("%#v\n", user.City)
}

@ -0,0 +1,25 @@
package main
import "fmt"
type Person3 struct {
name string
age int8
dreams []string
}
func (p *Person3) SetDreams(dreams []string) {
//p.dreams = dreams
p.dreams = make([]string, len(dreams))
copy(p.dreams, dreams)
}
func main() {
p1 := Person3{name: "小王子", age: 18}
data := []string{"吃饭", "睡觉", "打豆豆"}
p1.SetDreams(data)
// 你真的想要修改 p1.dreams 吗?
data[1] = "不睡觉"
fmt.Println(p1.dreams) // ?
}

@ -0,0 +1,33 @@
package main
import "fmt"
//Animal 动物
type Animal struct {
name string
}
func (a *Animal) move() {
fmt.Printf("%s会动\n", a.name)
}
//Dog 狗
type Dog struct {
Feet int8
*Animal //通过嵌套匿名结构体实现继承
}
func (d *Dog) wang() {
fmt.Printf("%s会汪汪汪~\n", d.name)
}
func main() {
d1 := &Dog{
Feet: 4,
Animal: &Animal{ //注意嵌套的是结构体指针
name: "乐乐",
},
}
d1.wang() //乐乐会汪汪汪~
d1.move() //乐乐会动!
}

@ -0,0 +1,33 @@
package main
/**
Tag Tag
`key1:"value1" key2:"value2"`
*/
import (
"encoding/json"
"fmt"
)
// Student2 学生
type Student2 struct {
ID int `json:"id"` //通过指定tag实现json序列化该字段时的key
Gender string //json序列化是默认使用字段名作为key
name string //私有不能被json包访问
}
func main() {
s1 := Student2{
ID: 1,
Gender: "男",
name: "沙河娜扎",
}
data, err := json.Marshal(s1)
if err != nil {
fmt.Println("json marshal failed!")
return
}
fmt.Printf("json str:%s\n", data) //json str:{"id":1,"Gender":"男"}
}
Loading…
Cancel
Save