<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] ## 技巧 ### 位運算做參數,類似`log.SetFlag()` <details> <summary>main.go</summary> ``` package main import ( "fmt" ) const ( A = 1<<iota B C ) func demo(flag int) { if flag&A != 0 { fmt.Printf("%+v\n", "A") } if flag&B != 0 { fmt.Printf("%+v\n", "B") } if flag&C != 0 { fmt.Printf("%+v\n", "C") } } func main() { demo(A | C) } ``` </details> <br /> ### 指針 ``` func main() { i := 10 ip := &i fmt.Printf("原始指針的內存地址是:%p\n", &ip) //0xc042072018 modify(ip) fmt.Println("int值被修改了,新值為:", i) //1 } func modify(ip *int) { fmt.Printf("函數里接收到的指針的內存地址是:%p\n", &ip) //0xc042072028 *ip = 1 } ``` > [參考](https://studygolang.com/articles/19386?utm_source=tuicool&utm_medium=referral) 1. 指針指向的數據都是在堆上分配的 2. 值的拷貝是昂貴的,所以用一個指針來代替 3. 如果可以證明它里面沒有指針,垃圾回收器會直接越過這塊內存 > 垃圾回收器回收一個變量時,要檢查該類型里是否有指針。 如果有,要檢查指針所指向的內存是否可被回收,進而才能決定這個變量能否被回收。如此遞歸下去。 如果被回收的變量里面沒有指針, 就不需要進去遞歸掃描了,直接回收掉就行 4. 如果結構體中沒有指針,則可編譯時知道,降低 gc 壓力的壓力 ### 條件編譯 目錄結構 ``` ├─echo ├─echo1.go └─echo2.go └─main.go ``` echo1.go ``` // +build !echo2 package echo func Echo() { fmt.Println("echo1.go") } ``` echo2.go ``` // +build echo2 package echo func Echo() { fmt.Println("echo1.go") } ``` main.go ``` func main() { echo.Echo() } ``` ``` go build -tags=echo2 main.go && main.exe //output echo2.go go build main.go && main.exe //output echo1.go ``` ### 中間件制作 以 http 接口為例子 ``` type minddleWarehandle struct { r *httprouter.Router } func NewMiddleWareHandle(r *httprouter.Router) minddleWarehandle { m := minddleWarehandle{r: r} return m } // !!! 覆寫 ServerHttp 接口 func (m minddleWarehandle) ServerHttp(w http.ResponseWriter, r *http.Request) { //check session ValidateUserSession(r) m.r.ServeHTTP(w, r) } func RegisetHandlers() *httprouter.Router { router := httprouter.New() router.POST("/user", CreateUser) router.POST("/user/:user_name", Login) return router } func main() { r := RegisetHandlers() mh := NewMiddleWareHandle(r)// 添加一層中間件 http.ListenAndServe(":8000", mh.r) } ``` ### 傳送二進文件,如視頻等 ``` video, e := os.Open(filepath) defer video.Close() if e != nil { log.Println("error") return } //把文件放入響應中 w.Header().Set("Content-Type", "video/mp4") http.ServeContent(w, r, "", time.Now(), video) ``` ### Read(p []byte) (n int, err error) 函數獲取全部數據 ``` //使用此函數 ioutil.ReadAll(r.Body.Read()) ``` ### 打印 log.print... 顯示文件與行數 ``` log.SetFlags(log.Lshortfile | log.LstdFlags) // output 2012/07/24 19:27:55 X.cgo1.go:14: ... ``` ### 使用 close 關閉 chan 每當使用完chan后,需關閉 >[參考](https://blog.csdn.net/len_yue_mo_fu/article/details/80315799) 實例一: ``` func write(ch1 chan int) { for i := 0; i < 10; i++ { ch1<-i } close(ch1) //當復制完close 時,需要關閉 } ``` 實例二: ``` quit := make(chan bool) go func() { for { select { case <-quit: // closed channel 不會阻塞,因此可?作退出通知。 println("he") return default: // 執?正常任務。 println(time.Now().Nanosecond()) time.Sleep(time.Second) } } }() time.Sleep(2 * time.Second) close(quit) // 發出退出通知。 time.Sleep(2 * time.Second) ``` ### iota 創建 字節大小 ``` const ( _ = 1 << (10 * iota) KB // 1024 MB // 1048576 GB // 1073741824 TB // 1099511627776 (exceeds 1 << 32) PB // 1125899906842624 EB // 1152921504606846976 ZB // 1180591620717411303424 (exceeds 1 << 64) YB // 1208925819614629174706176 ) ``` ### 遞歸函數中傳遞切片 ``` func sliceModify(slice *[]int) { *slice = append(*slice, 6) } func main() { slice := []int{1, 2, 3, 4, 5} sliceModify(&slice) fmt.Println(slice) // [1 2 3 4 5 6] } ``` ### 自定義tag標簽(reflect) ``` type Student struct { Name string `demo:"user_name"` Age int Score float32 `demo:"sc"` } func test(a interface{}) { v := reflect.TypeOf(a) numFiled := v.NumField() //獲取filed 數量 for i := 0; i < numFiled; i++ { value, ok := v.Field(i).Tag.Lookup("demo") if ok { fmt.Println(value) // user_name sc } } } func main() { var a = Student{ Name: "stu01", Age: 18, Score: 92, } test(a) } ``` ### struct 的值模式與指針模式 > [參考](https://mp.weixin.qq.com/s/11wxINd_RA40YD0bL2Zbqg) 1. 數據分配密集型 選用值模式 2. 方法調用密集型(方法平凡調用) 選中指針模式 ### 整數轉二進制 ``` var a int64 =1 fmt.Printf("%08b\n",a ) //00000001 整數轉8位二進制 fmt.Printf("%010b\n",a ) //0000000001 整數轉108位二進制 ``` ### chan struct{} 創建操作信號 struct{} 是內存最小單位,chan bool 次之,interface{}最差 ``` a:=make(chan struct{}) go func() { time.Sleep(1*time.Second) close(a) }() <-a ``` ### go func(v string){}(v) 在循環中需要傳入參數 error ``` var url=[]string{"http://www.baidu.com","http://www.bilibili.com","http://www.baidu.com",} for _, v := range url { go func() { fmt.Printf("%+v\n", v) }() } time.Sleep(3*time.Second) //output http://www.baidu.com http://www.baidu.com http://www.baidu.com ``` good ``` var url=[]string{"http://www.baidu.com","http://www.bilibili.com","http://www.baidu.com",} for _, v := range url { go func(v string) { fmt.Printf("%+v\n", v) }(v) } time.Sleep(3*time.Second) //output http://www.baidu.com http://www.bilibili.com http://www.baidu.com ``` ### 退出多個循環 ``` func main() { L1: for x := 0; x < 3; x++ { L2: for y := 0; y < 5; y++ { if y > 2 { continue L2 } if x > 1 { break L1 } //退出多個循環,與 goto 類似 print(x, ":", y, " ") } println() } } //output //0:0 0:1 0:2 //1:0 1:1 1:2 ``` ### 跳出 for select 循環 1. return ``` for { select { case <-quit: //this.Close() return case msg := <-this.sendChan: if msg.QuictClient { this.quit<- struct{}{} } default: time.Sleep(1 * time.Second) } log.Print("1") } log.Print("2") ``` 2. break loop ``` LOOP:for { select { case <-quit: case msg := <-this.sendChan: break LOOP default: time.Sleep(1 * time.Second) } log.Print("1") } log.Print("2") ``` 3. goto ``` for { select { case <-quit: case msg := <-this.sendChan: goto LOOP default: time.Sleep(1 * time.Second) } log.Print("1") } LOOP: log.Print("2") ``` ## var Config sturct{} 存儲配置文件 bad ``` type config struct{ name string } var Config config ``` good ``` var Config struct{ name string } ``` > 注意: 如果 sturct 有方法則不能使用此
                  <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>

                              哎呀哎呀视频在线观看