当前位置:网站首页>Go Language Advanced
Go Language Advanced
2022-07-01 19:27:00 【Game programming】
aggregate , passageway , section , Error handling , Interface
package mainimport ( "fmt" "time")//8 Member variables var i intvar s bool = falsefunc main() { //1 variable Statement //go Medium String s To be lowercase, it has no concept of class var s string = " Lin daiyu " var a int = 1 var b int = 1 fmt.Println(a + b) fmt.Println("hello world!") fmt.Println(s) var c, d int = 1, 2 fmt.Println(c + d) //2 Variable declarations const e, f = " Xue Baochai ", " Jia Baoyu " fmt.Println(e + f) //3 Judgment statement if s == " Lin daiyu " { fmt.Println("111") } else { fmt.Println("222") } //4 Loop statement for i := 0; i < 5; i++ { fmt.Println(i) } /*for true { fmt.Println(" Sister Lin ") }*/ // 7 Function call var res int = max(a, d) fmt.Println(res) // 9 Array var arr = [4]int{1, 24, 45, 6} fmt.Println(arr) i2 := arr[0] fmt.Println(i2) for i := 0; i < len(arr); i++ { fmt.Println(arr[i]) } for i3 := range arr { fmt.Println(i3) } //10 Memory address var m int = 23 //& yes go To address symbol in Memory address fmt.Println("m The address value in memory is :", &m) //m The address value in memory is : 0xc000018140 //11 The pointer : A pointer variable points to the memory address of a value . //12 Statement //var r *float32 // Defining pointer variables . var m1 int = 12 // Assign values to pointer variables . var p *int // The pointer fmt.Println("m1 The address value in memory is ", m1) fmt.Println("p The address value in memory is :", &p) // Access the value pointing to the address in the pointer variable . p = &m1 // The pointer points to 12 // Accessing values through pointers fmt.Println("p Value :", *p) // Null pointer var point *int // according to for Cycle to understand fmt.Println(" Null pointer :", point) // Null pointer : <nil> //14 Instantiate a structure fmt.Println(" New structure :", Books{" Cao xueqin ", " A dream of red mansions ", " philology ", 1}) // Instantiate a structure type ( Create objects ) var Book1 Books var Book2 Books //get set Method to assign a value to a property Book1.title = " Journey to the west " Book1.anthor = " Wu chengen " Book2.book_id = 1333 Book2.subject = " War class " fmt.Println(Book1) fmt.Println(Book2) // Structure as function parameter print(Book2) //15 section , Expand the capacity of the array ( It's like a dynamic array ) var slice []int = make([]int, 3) slice = make([]int, 3) // Abbreviation slice = []int{1, 3, 5} //init fmt.Println(slice) //[1 3 5] var nums = make([]int, 3, 5) //t,len,cap printMethod(nums) //3 5 [0 0 0] Empty section nil // Slice off /*var array = [5]int{1, 2, 1, 3, 8} var slice01 = make(arr, 4, 6)*/ // Create slices slice01 := []int{1, 2, 1, 3, 8} printMethod(slice01) fmt.Println(" Original slice ", slice01) // Original slice [1 2 1 3 8] fmt.Println(" Print from index 1-4 All elements of , contain 1, But does not contain 4", slice01[1:4]) //append()() var nums01 []int // Define a slice printMethod(nums01) //0 0 [] nums01 = append(nums01, 1) nums01 = append(nums01, 6) fmt.Println(nums01) //[1 6] //copy() slice02 := make([]int, len(nums01), cap((nums01))*2) fmt.Println(slice02) copy(slice02, nums01) //nums01 copy to slice02 printMethod(slice02) //2 4 [1 6] //16 enhance for loop var array01 = []int{1, 2, 1, 3, 8} for i3 := range array01 { fmt.Println(i3) } map1 := make(map[int]float32) map1[0] = 1.0 map1[1] = 2.0 map1[2] = 3.0 map1[3] = 4.0 map1[4] = 1.0 for key, val := range map1 { fmt.Println(key, val) } for key := range map1 { fmt.Println(key) } for _, val := range map1 { fmt.Println(val) }}func printMethod(num []int) { fmt.Println(len(num), cap(num), num)}func print(books Books) { fmt.Println(books.book_id) fmt.Println(books.subject) fmt.Println(books.title) fmt.Println(books.anthor) //17map aggregate var map2 map[string]string map2 = make(map[string]string) map2[" Rongguofu "] = " Jia Baoyu " map2[" Ning Guofu "] = " Jia Xichun " fmt.Println(map2) //map[ Ning Guofu : Jia Xichun Rongguofu : Jia Baoyu ] delete(map2, " Ning Guofu ") fmt.Println(map2) //map[ Rongguofu : Jia Baoyu ] //18 Recursive function //method() //fatal error: stack overflow //Go Language supports recursion . But when we use recursion , Developers need to set exit conditions , Otherwise, recursion will fall into an infinite loop . // Recursive functions are very useful for solving mathematical problems , It's like calculating factorials , Generating Fibonacci series, etc . fmt.Println("12 The factorial is :", Factorial(12)) //479001600 for i := 0; i < 10; i++ { fmt.Println(fibonacci(i)) //0 1 1 2 3 //5 //8 //13 //21 //34 } //19 Type conversion var a1 int = 17 var b1 int = 5 var res float32 res = float32(a1) / float32(b1) fmt.Println(res) //3.4 // Implicit conversion is not supported , That is, big to small //20go Language interface var red Red red = new(NingRed) red.sleep() red = new(RongRed) red.sleep() // Error handling // The denominator is not 0 if result, errorMsg := Divide(4, 2); errorMsg == "" { fmt.Println(result) } // The denominator is 0 if _, errorMsg := Divide(2, 0); errorMsg != "" { fmt.Println(errorMsg) } //21go Concurrent go say(" Jia Baoyu ") //go Must be in say Before call, Otherwise, it will always be say say(" Lin ") // Execute the above code , You'll see the output hello and world // There is no fixed order . Because they are two goroutine In execution : // passageway // establish channel chdemo := make(chan int) fmt.Println(chdemo) //0xc00018a000 s := []int{1, 2, 4, 5, 6} ch := make(chan int) go sum(s[:len(s)/2], ch) go sum(s[len(s)/2:], ch) m, n := <-ch, <-ch fmt.Println(m, n, m+n) sum(s, ch) //all goroutines are asleep - deadlock! // buffer /* The channel with buffer allows the data transmission of the sender and the data acquisition of the receiver to be in an asynchronous state , In other words, the data sent by the sender can be put in the buffer , You can wait for the receiver to get the data , Instead of requiring the receiver to get the data immediately .*/ ch02 := make(chan int, 2) ch03 := make(chan int, 3) ch02 <- 4 ch03 <- 6 fmt.Println(<-ch02) fmt.Println(<-ch03) // Traversal channel c := make(chan int, 10) go itator(cap(c), c) for i := range c{ fmt.Println(i) }}func itator(a int, ch chan int) { x, y := 1, 2 for i := 0; i < a; i++ { ch <- x x, y = y, x+y } close(ch)}func sum(arr []int, ch01 chan int) { sum := 0 for i5 := range arr { sum += i5 ch01 <- sum // hold sum Assign to ch01 }}func say(s string) { for i := 0; i < 5; i++ { time.Sleep(time.Millisecond * 100) fmt.Println("---", s) }}// Define an error structure type errorDemo struct { demo1 int demo2 int}func (de *errorDemo) Error() string { strFormat := ` 0 Cannot be used as denominator demo1 = %d demo2 = 0` return fmt.Sprintf(strFormat, de.demo1)}func Divide(a3 int, b3 int) (result int, errorMsg string) { if b3 == 0 { data := errorDemo{ demo1: a3, demo2: b3, } errorMsg = data.Error() return } else { return a3 / b3, "" }}type Red interface { sleep()}type NingRed struct {}func (ningRed NingRed) sleep() { fmt.Println(" There is ningguofu in the dream of Red Mansions , Shi Xiangyun lies drunk with peony root ")}type RongRed struct {}func (rongRed RongRed) sleep() { fmt.Println(" There is Rongguo mansion in the dream of Red Mansions , Yang Fei plays with butterflies in the teary Pavilion ")}func fibonacci(n int) int { if n < 2 { return n } return fibonacci(n-2) + fibonacci(n-1)}func Factorial(n uint64) (result uint64) { if n > 0 { result = n * Factorial(n-1) return result } return 1}func method() { method()}//13 Structure ( amount to java Objects in the )type Books struct { // Structure definition needs to use type and struct sentence .struct // Statement to define a new data type , A structure has one or more members .type Statement sets the name of the structure . The format of the structure is as follows : // Defining a structure cannot use var anthor string title string subject string book_id int}// 6 Function declaration func max(num1, num2 int) int { /* Declare local variables */ var result int if num1 > num2 { result = num1 } else { result = num2 } return result}
author : Tianlu burning Chenopodium
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- CDGA|从事通信行业,那你应该考个数据管理证书
- Solution: you can ping others, but others can't ping me
- 有关 M91 快速霍尔测量仪的更多信息
- nacos配置文件发布失败,请检查参数是否正确的解决方案
- 案例分享:QinQ基本组网配置
- Team up to learn! 14 days of Hongmeng equipment development "learning, practicing and testing" practical camp, free of charge!
- 【森城市】GIS数据漫谈(一)
- Getting started with kubernetes command (namespaces, pods)
- 寶,運維100+服務器很頭疼怎麼辦?用行雲管家!
- Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference
猜你喜欢
Docker deploy mysql8.0
苹果产品在日本全面涨价,iPhone13涨19%
小红书上的爱情买卖
Lake Shore低温恒温器的氦气传输线
M91 fast hall measuring instrument - better measurement in a shorter time
Getting started with kubernetes command (namespaces, pods)
C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
Cdga | if you are engaged in the communication industry, you should get a data management certificate
Learn MySQL from scratch - database and data table operations
随机推荐
Transform + ASM data
XML syntax, constraints
M91快速霍尔测量仪—在更短的时间内进行更好的测量
赋能「新型中国企业」,SAP Process Automation 落地中国
MySQL常用图形管理工具 | 黑马程序员
华为游戏初始化init失败,返回错误码907135000
Chinese and English instructions human soluble advanced glycation end products receptor (sRAGE) ELISA Kit
PMP是被取消了吗??
Dlib+opencv library for fatigue detection
组队学习! 14天鸿蒙设备开发“学练考”实战营限时免费加入!
Lake shore M91 fast hall measuring instrument
Witness the times! "The future of Renji collaboration has come" 2022 Hongji ecological partnership conference opens live broadcast reservation
论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】
Learning notes [Gumbel softmax]
Mipi interface, DVP interface and CSI interface of camera [easy to understand]
见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
【org.slf4j.Logger中info()方法】
Three simple methods of ES6 array de duplication
寶,運維100+服務器很頭疼怎麼辦?用行雲管家!
June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries