<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ## 整數 ## 數字類型 變量賦值時,一般都采用有符號整型 1. 無符號整型 ``` uint8 //1 個字節 0-255 uint16 //2 個字節 0-65535 uint32 //3 個字節 0-4294967295 uint64 //3 個字節 0-18446744073709551615 ``` 2. 有符號整型 ``` int8 //1 個字節 -128-127 int16 //2 個字節 -32768-32767 int32 //3 個字節 -2147483648-2147483647 int64 //3 個字節 -9223372036854775808-9223372036854775807 ``` ## 浮點型 變量賦值時,一般都采用float64,float32 在計算時候容易缺失精度 ``` float32 //32位浮點型數 float64 //64位浮點型數 complex64 //32 位實數和虛數 complex128 //64 位實數和虛數 ``` ## 其他數字類型 ``` byte //uint8 rune //int32 uint //32 或 64 位 根據系統決定 int //與 uint 一樣大小 uintptr //無符號整型,用于存放一個指針 ``` ## 字符串類型 ``` hw1 := "世界" hw2 := "\xe4\xb8\x96\xe7\x95\x8c" hw3 := "\u4e16\u754c" hw4 := "\U00004e16\U0000754c" // 它們的值都是相同的 fmt.Printf("%+v\n", hw1) // 世界 fmt.Printf("%+v\n", hw2) // 世界 fmt.Printf("%+v\n", hw3) // 世界 fmt.Printf("%+v\n", hw4) // 世界 ``` - 對于小于256碼點值可以寫在一個十六進制轉義字節中,例如\x41對應字符'A',但是對于更大的碼點則必須使用\u或\U轉義形式。因此,`\xe4\xb8\x96` ( 世 ) 并不是一個合法的rune字符,雖然這三個字節對應一個有效的UTF8編碼的碼點 ## 指針類型( Pointer) ### 指針賦值 ``` var a = 3 var p = &a //取內存值 *p = 4 fmt.Println(a) //0xc042052080 fmt.Println(p) //4 ``` ### 指針傳參 ``` func swap(a *int) { *a++ } a := 3 swap(&a) fmt.Println(a) //4 ``` ## 數組類型 (在 go 語言中一般不使用數組,推薦切片) ``` //初始化賦值 var a[5] a[0]=12 var balance = [5]float32{1000.0, 2.0, 3.4, 7.0, 50.0} //自動識別長度 var balance = [...]float32{1000.0, 2.0, 3.4, 7.0, 50.0} //多維數組 b :=[][]int { {1,2,3}, {3,24,35}, {11,22,33}, } ``` ``` type Currency int const ( USD Currency = iota // 美元 EUR // 歐元 GBP // 英鎊 RMB // 人民幣 ) symbol := [...]string{USD: "$", EUR: "€", GBP: "£", RMB: "¥"} fmt.Println(RMB, symbol[RMB]) // "3 ¥" ``` ## Channe|類型(chan) 不能超過設置的長度 ``` person := make(chan int) //默認的長度只有1,超出后報錯 person <- 12 person <- 23 //fatal error: all goroutines are asleep - deadlock! ``` ### chan 返回最快結果的用法 多個goroutines并發地向同一個channel發送數據,或從同一個channel接收數據都是常見的用法 ``` func mirroredQuery() string { responses := make(chan string, 3) go func() { responses <- request("asia.gopl.io") }() go func() { responses <- request("europe.gopl.io") }() go func() { responses <- request("americas.gopl.io") }() return <-responses // return the quickest response } ``` ### 一個chan 可以同時被多個 select 監聽到 <details> <summary>server.go</summary> ``` package main import ( "fmt" "time" ) func main() { ch :=make(chan struct{}) go task(ch,"1") go task(ch,"2") go task(ch,"3") time.Sleep(3 * time.Second) close(ch) time.Sleep(10 * time.Second) } func task(ch chan struct{},num string) { func() { for { select { case <-ch: fmt.Printf("quit - %+v\n", num) return default: } fmt.Printf("%+v\n", num) time.Sleep(1 * time.Second) } }() } // 輸出 //2 //1 //3 //3 //1 //2 //quit - 2 //quit - 3 //quit - 1 92. ``` </details> <br/> ## 函數類型(func) ``` //int 類型也可改為 a ...interface{} 傳 demo(3,4,"5") func demo(a ...int){ fmt.Print(a) //[3 4 5] } demo(3,4,5) ``` ### 復雜傳值 ``` func demo(a ...interface{}){ fmt.Print(a) //[3 4 5 [3 4 5]] } a :=[]interface{}{3,4,5} demo(3,4,5,a) ``` ### 函數參數為數組時的長度需相等 ``` func demo(a [5]int){ fmt.Println(a) } //ok a :=[5]int{1,2,3,4,5} demo(a) //error a :=[3]int{1,2,3} demo(a) ``` ### 函數當作參數傳值 ``` func compute(fn func(int, int) int) int { return fn(3, 4) } func main() { c := compute(add) fmt.Println(c) } func add(a, b int) int { return a + b } ``` ### 函數當參數 ``` type Callback func(x, y int) int ``` ### 閉包函數 - 實現斐波那契數組 函數返回前的變量不會改變 ``` func fibonacci1() func() int { back1, back2 := 0, 1 // 該值初始化一次,return 中把 back1,back2的變化會保留下來 return func() int { temp := back1 back1, back2 = back2, (back1 + back2) //返回temp return temp } } func main() { f := fibonacci1() // for i := 0; i < 10; i++ { // 檢測下前10個值 fmt.Println(f()) } } ``` ## 接口實現 ### 接口與隱式實現 無需聲明某個結構體實現了接口,當結構體滿足接口時,即完成了接口的實現 ``` type I interface { M() } type T struct { S string } // 此方法表示類型 T 實現了接口 I,但我們無需顯式聲明此事。 func (t T) M() { fmt.Println(t.S) } ``` ### 空接口( interface) - 指定了零個方法的接口值 創建符合接口的結構體 ``` //定義phone的接口必須要有call type Phone interface { call() } //構建結構體 type NokiaPhone struct { } func (nokiaPhone NokiaPhone) call() { fmt.Println("I am Nokia, I can call you!") } func main() { var phone Phone phone = new(NokiaPhone) //接頭體傳入接口 如果不存在call() 則new 會保存 phone.call() fmt.Println(reflect.TypeOf(phone)) } ``` ### interface 可以當任何值 ``` // 定義a為空接口 var a interface{} var i int = 5 s := "Hello world" // a可以存儲任意類型的數值 a = i a = s ``` ### interface變量存儲的類型 可以直接判斷是否是該類型的變量:` value, ok = element.(T)`,這里value就是變量的值,ok是一個bool類型,element是interface變量,T是斷言的類型。 ``` ype Element interface {} type List [] Element type Person struct { name string age int } func main() { list := make(List,3) list[0] = 1 // an int list[1] = "Hello" // a string list[2] = Person{"Dennis", 70} for index, element := range list { if value, ok := element.(int); ok { fmt.Printf("list[%d] is an int and its value is %d\n", index, value) } else if value, ok := element.(string); ok { fmt.Printf("list[%d] is a string and its value is %s\n", index, value) } else if value, ok := element.(Person); ok { fmt.Printf("list[%d] is a Person and its value is %s\n", index, value) } else { fmt.Printf("list[%d] is of a different type\n", index) } } } ``` ### 接口實現 io.Write ``` type ByteCounter int func (c *ByteCounter) Write(p []byte) (int, error) { *c += ByteCounter(len(p)) // convert int to ByteCounter return len(p), nil } func main() { var c ByteCounter fmt.Fprintf(&c, "hello, %s", "Dolly") fmt.Println(c) // "12", = len("hello, Dolly") } ``` ## Map類型(map) 1. 初始化并賦值 ``` var countryCapitalMap map[string]string /*創建集合 */ countryCapitalMap = make(map[string]string) // or countryCapitalMap := make(map[string]string) ``` 2. 刪除 ``` countryCapitalMap := make(map[string]string) countryCapitalMap [ "France" ] = "Paris" //刪除 delete(countryCapitalMap, "France") ``` 3. 存在鍵判斷鍵是否 ``` elem, ok = m[key] ``` 函數傳參 ``` func main() { args :=make(map[int]string,0) args[0]="1" demo(args) fmt.Printf("%#v\n", args) // map[int]string{0:"1", 2:"2"} demo1(args) fmt.Printf("%#v\n", args) // map[int]string{0:"-1", 2:"2"} } // 在切片中追加,無效 func demo(args map[int]string){ args[2]="2" } // 可修改已有切片的值 func demo1(args map[int]string){ args[0]="-1" args=nil // 賦值 nil 也無效 } ``` ## 結構化類型(struct) 可比較的結構體可用于 map ``` type address struct { hostname string port int } hits := make(map[address]int) hits[address{"golang.org", 443}]++ ``` 在其他結構體中共享結構體 ``` type Point struct { X,Y int } type ColoredPoint struct { *Point Z int } p := Point{X: 1, Y: 2} point1 := ColoredPoint{&p,3} point2 := ColoredPoint{&p,4} p.X++ fmt.Printf("%+v\n", point1.X) // 2 fmt.Printf("%+v\n", point2.X) // 2 ``` ## 反射 ``` t := reflect.TypeOf(i) //得到類型的元數據,通過t我們能獲取類型定義里面的所有元素 v := reflect.ValueOf(i) //得到實際的值,通過v我們獲取存儲在里面的值,還可以去改變值 ``` ## 類型斷言 1. 使用 switch 斷言 ``` var a interface{} a = 123.1 switch value := a.(type) { case int: fmt.Printf("int : %v", value) case float64: fmt.Printf("float : %v", value) case string: fmt.Printf("string : %v", value) case bool: fmt.Printf("bool : %v", value) default: fmt.Printf("not found") } ``` 2. 使用反射 (推薦) ``` a = 123.1 of := reflect.TypeOf(a) fmt.Println(of) //float64 ``` ## 特殊常量iota概念 1. iota在 const關鍵字出現時將被重置為0; 2. const中每新增一行常量聲明將使iota計數一次; 3. iota常見使用法 ``` 1.跳值使用法 const ( C = iota //0 D = iota //1 _ E = iota //3 ) 2.插隊使用法 const ( C=iota //0 D=iota //1 B="asd" E =iota //3 ) 3.表達式隱式使用法 const ( C=iota*2 //0 D //1*2=2 B //2*2=4 ) const ( _ = 1 << (10 * iota) KiB // 1024 MiB // 1048576 GiB // 1073741824 TiB // 1099511627776 (exceeds 1 << 32) PiB // 1125899906842624 EiB // 1152921504606846976 ZiB // 1180591620717411303424 (exceeds 1 << 64) YiB // 1208925819614629174706176 ) 4.單行使用法 const ( A,B=iota,iota*3 //A=0 B=0 C,D //C=1 D=3 ) ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看