D:\code_gitee\go_example\main.go
| package main |
| |
| import ( |
| "fmt" |
| ) |
| |
| func main() { |
| |
| const a string = "hello" |
| |
| const b = "hello" |
| |
| const x, y, z = 1, true, "hello" |
| fmt.Println("x=", x, "y=", y, "z=", z) |
| fmt.Println("----------------------") |
| |
| |
| const ( |
| Spring = 0 |
| Summer = 1 |
| Autumn = 2 |
| Winter = 3 |
| ) |
| fmt.Println(Spring) |
| fmt.Println(Summer) |
| fmt.Println(Autumn) |
| fmt.Println(Winter) |
| fmt.Println("----------------------") |
| |
| |
| const ( |
| x1 = "hello" |
| y1 = len(b) |
| ) |
| fmt.Println("x1=", x1) |
| fmt.Println("y1=", y1) |
| fmt.Println("----------------------") |
| |
| |
| const ( |
| Morning = iota |
| Afternoon |
| Evening |
| ) |
| fmt.Println(Morning) |
| fmt.Println(Afternoon) |
| fmt.Println(Evening) |
| fmt.Println("----------------------") |
| |
| |
| const ( |
| Red = 1 << iota |
| Orange |
| Yellow |
| Green |
| ) |
| fmt.Println(Red) |
| fmt.Println(Orange) |
| fmt.Println(Yellow) |
| fmt.Println(Green) |
| fmt.Println("----------------------") |
| |
| |
| |
| type ByteSize float64 |
| const ( |
| _ = iota |
| KB ByteSize = 1 << (10 * iota) |
| MB |
| GB |
| TB |
| PB |
| EB |
| ZB |
| YB |
| ) |
| fmt.Println(KB) |
| fmt.Println(MB) |
| fmt.Println(GB) |
| fmt.Println("----------------------") |
| |
| |
| const ( |
| Apple, Banana = iota + 1, iota + 2 |
| Pinia, Bluebarry |
| Peach, Leomon |
| ) |
| fmt.Println(Apple) |
| fmt.Println(Banana) |
| fmt.Println(Pinia) |
| |
| } |
| |
D:\code_gitee\go_example\main01_初次使用.go.txt
| package main |
| |
| import ( |
| "fmt" |
| |
| "com.song.learn/models" |
| "com.song.learn/utils" |
| ) |
| |
| func main() { |
| per := models.Person{ |
| Name: "Alice", |
| Age: 19, |
| } |
| fmt.Println(per) |
| fmt.Println("------------------------") |
| |
| stu := models.Student{ |
| Name: "Bruce", |
| Age: 19, |
| Grade: 3, |
| } |
| fmt.Println(stu) |
| fmt.Println("-----------------------") |
| |
| res := utils.Sum(1, 3) |
| fmt.Println("sum(1,3)=", res) |
| } |
| |
D:\code_gitee\go_example\main02_ICM报文封装_不会.go.txt
| package main |
| |
| import ( |
| "bytes" |
| "encoding/binary" |
| "fmt" |
| "net" |
| ) |
| |
| type ICMP struct { |
| Type uint8 |
| Code uint8 |
| Checksum uint16 |
| Identifier uint16 |
| SequenceNum uint16 |
| } |
| |
| func CheckSum(data []byte) uint16 { |
| var ( |
| sum uint32 |
| length int = len(data) |
| index int |
| ) |
| for length > 1 { |
| sum += uint32(data[index])<<8 + uint32(data[index+1]) |
| index += 2 |
| length -= 2 |
| } |
| if length > 0 { |
| sum += uint32(data[index]) |
| } |
| sum += (sum >> 16) |
| |
| return uint16(^sum) |
| } |
| |
| func main() { |
| var ( |
| icmp ICMP |
| laddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.1.2")} |
| raddr net.IPAddr = net.IPAddr{IP: net.ParseIP("192.168.1.1")} |
| ) |
| |
| |
| conn, err := net.DialIP("ip4:icmp", &laddr, &raddr) |
| if err != nil { |
| fmt.Println(err.Error()) |
| return |
| } |
| defer conn.Close() |
| |
| |
| icmp.Type = 8 |
| icmp.Code = 0 |
| icmp.Checksum = 0 |
| icmp.Identifier = 0 |
| icmp.SequenceNum = 0 |
| |
| var ( |
| buffer bytes.Buffer |
| ) |
| |
| binary.Write(&buffer, binary.BigEndian, icmp) |
| icmp.Checksum = CheckSum(buffer.Bytes()) |
| |
| buffer.Reset() |
| binary.Write(&buffer, binary.BigEndian, icmp) |
| |
| if _, err := conn.Write(buffer.Bytes()); err != nil { |
| fmt.Println(err.Error()) |
| return |
| } |
| fmt.Printf("send icmp packet success!") |
| } |
| |
D:\code_gitee\go_example\main03_变量.go.txt
| package main |
| |
| import ( |
| "fmt" |
| "reflect" |
| ) |
| |
| |
| var gx,gy int |
| |
| |
| var ( |
| gm int = 110 |
| gn bool = true |
| ) |
| |
| |
| |
| |
| func main() { |
| |
| var a int |
| fmt.Printf("a=%d", a) |
| fmt.Println("------------------") |
| var b string |
| fmt.Printf("b=%s", b) |
| fmt.Println("------------------") |
| |
| |
| var c int = 100 |
| fmt.Println(c) |
| fmt.Printf("type(c)= %T\n", c) |
| |
| fmt.Println("type(c)=", reflect.TypeOf(c)) |
| fmt.Println("------------------") |
| |
| |
| var d = 20 |
| fmt.Printf("type(d)=%T\n", d) |
| fmt.Println("------------------") |
| |
| |
| e := 3.14 |
| fmt.Printf("e=%f\n", e) |
| fmt.Printf("type of e=%T\n", e) |
| fmt.Println("------------------") |
| |
| |
| m, n := 123, 456 |
| fmt.Println("m=", m) |
| fmt.Println("n=", n) |
| fmt.Println("------------------") |
| |
| var m1, n1 = 123, 456 |
| var m11, n11 int = 123, 456 |
| fmt.Println("m1=", m1) |
| fmt.Println("n1=", n1) |
| fmt.Println("m11=", m11) |
| fmt.Println("n11=", n11) |
| fmt.Println("------------------") |
| |
| var m2, n2 = 123, "test" |
| |
| fmt.Printf("m2=%T\n", m2) |
| fmt.Printf("n2=%T\n", n2) |
| fmt.Println("------------------") |
| |
| |
| _, y := 123, 456 |
| |
| fmt.Println("y=", y) |
| fmt.Println("------------------") |
| |
| |
| fmt.Println("gx=",gx,"gy=",gy) |
| fmt.Println("gm= ",gm,"gn= ",gn) |
| } |
| |
D:\code_gitee\go_example\main04_常量.go.txt
| package main |
| |
| import ( |
| "fmt" |
| ) |
| |
| func main() { |
| |
| const a string = "hello" |
| |
| const b = "hello" |
| |
| const x, y, z = 1, true, "hello" |
| fmt.Println("x=", x, "y=", y, "z=", z) |
| fmt.Println("----------------------") |
| |
| |
| const ( |
| Spring = 0 |
| Summer = 1 |
| Autumn = 2 |
| Winter = 3 |
| ) |
| fmt.Println(Spring) |
| fmt.Println(Summer) |
| fmt.Println(Autumn) |
| fmt.Println(Winter) |
| fmt.Println("----------------------") |
| |
| |
| const ( |
| x1 = "hello" |
| y1 = len(b) |
| ) |
| fmt.Println("x1=", x1) |
| fmt.Println("y1=", y1) |
| fmt.Println("----------------------") |
| |
| |
| const ( |
| Morning = iota |
| Afternoon |
| Evening |
| ) |
| fmt.Println(Morning) |
| fmt.Println(Afternoon) |
| fmt.Println(Evening) |
| fmt.Println("----------------------") |
| |
| |
| const ( |
| Red = 1 << iota |
| Orange |
| Yellow |
| Green |
| ) |
| fmt.Println(Red) |
| fmt.Println(Orange) |
| fmt.Println(Yellow) |
| fmt.Println(Green) |
| fmt.Println("----------------------") |
| |
| |
| |
| type ByteSize float64 |
| const ( |
| _ = iota |
| KB ByteSize = 1 << (10 * iota) |
| MB |
| GB |
| TB |
| PB |
| EB |
| ZB |
| YB |
| ) |
| fmt.Println(KB) |
| fmt.Println(MB) |
| fmt.Println(GB) |
| fmt.Println("----------------------") |
| |
| |
| const ( |
| Apple, Banana = iota + 1, iota + 2 |
| Pinia, Bluebarry |
| Peach, Leomon |
| ) |
| fmt.Println(Apple) |
| fmt.Println(Banana) |
| fmt.Println(Pinia) |
| |
| } |
| |
D:\code_gitee\go_example\models\Person.go
| package models |
| |
| type Person struct { |
| Name string |
| Age int |
| } |
| |
D:\code_gitee\go_example\models\Student.go
| package models |
| |
| type Student struct { |
| Name string |
| Age int |
| Grade int |
| } |
| |
D:\code_gitee\go_example\utils\sum.go
| package utils |
| |
| |
| func Sum(a int, b int) int { |
| return a + b |
| } |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验