Go FAQ

Go FAQ

GO FAQ

proxy.golang.org timeout

1
2
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn

split with multiple splitter多分隔符切割

eg1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  package main

  import (
          "fmt"
          "strings"
  )
  func splitter1(str string) []string{
          r_strs := strings.Replace(str, " ", ",", -1)
          strs := strings.Split(r_strs, ",")
          return strs
  }

  func main() {
          str1 := splitter1("hello, i am ok")
          fmt.Println("hello, i am ok will be:", str1)
          for k, v := range str1 {
                  fmt.Println(k, ":", v)
          }

  }
hello, i am ok will be: [hello  i am ok]
0 : hello
1 :
2 : i
3 : am
4 : ok

eg2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  package main

  import (
          "fmt"
          "strings"
  )
  func splitter2(str string, seqs string) []string{
        m := make(map[rune]int)

        for _, r := range seqs {
                m[r] = 1
        }

        splitter := func(r rune) bool {
                return m[r] == 1
        }

        return strings.FieldsFunc(str, splitter)
  }

  func main() {
          str2 := splitter2("hello, i am ok", ", ")
          fmt.Println("hello, i am ok will be:", str2)
          for k, v := range str2 {
                  fmt.Println(k, ":", v)
          }
  }
hello, i am ok will be: [hello i am ok]
0 : hello
1 : i
2 : am
3 : ok

eg3:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
  package main

  import (
          "regexp"
          "fmt"
  )
  func splitter3(str string) []string {
          strs := regexp.MustCompile("[\\,\\s]").Split(str, -1)
          for index, s := range strs {
                  fmt.Println(index, ":", s)
          }
          return strs
  }

  func main() {
          str3 := splitter3("hello, i am ok")
          fmt.Println("hello, i am ok will be:", str3)
  }
0 : hello
1 :
2 : i
3 : am
4 : ok
hello, i am ok will be: [hello  i am ok]

总结

正则和替换实现简单,但是需要去空格 第二个例子rune相关请参考Go Basics

http相关报错

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
2021/10/09 10:53:12 server.go:535: WARN : rpcx: failed to handle request: [service internal error]: runtime error: invalid memory address or nil pointer dereference, method: HttpGet, argv: &{Method:Get Data: Url:http://op.liuliancao.com/api/v1/host/9}, stack: goroutine 28 [running]:
github.com/smallnest/rpcx/server.(*service).call.func1()
/home/liuliancao/go/pkg/mod/github.com/smallnest/rpcx@v1.6.11/server/service.go:334 +0xcd
panic({0x99d000, 0xce5630})
/usr/local/go/src/runtime/panic.go:1047 +0x262
guarder/pkg/http.HttpPost({0xc0000b2870, 0xe}, {0x99aa20, 0xc00019ac90}, {0xa08631, 0x10})
/home/liuliancao/projects/guarder/pkg/http/http.go:85 +0x682
guarder/pkg/http.GetToken()
/home/liuliancao/projects/guarder/pkg/http/http.go:53 +0x1f3
guarder/pkg/http.CMDBHttpGET({0xc0000c6420, 0x1e})
/home/liuliancao/projects/guarder/pkg/http/http.go:134 +0x65
fguarder/pkg/http.(*HttpForwarder).HttpGet(0xc00013c560, {0xa51eb8, 0xc0000a63c0}, 0xc00019ab10, 0xc00019abd0)

这里其实要注意的是,http相关url前缀一定要带上协议,比如http还是https,这个是url没有写http导致的

http相关报错json死活绑定不上

结果查了半天发现是绑定的时候写的ShouldBind而不是BindJSON c.BindJSON(&g) //写成了c.ShouldBind(&g)

emacs里面go语法检查错误,在引入外包的时候

执行go get github.com/xxx/xxx

rpcx报错rpcx: client protocol error:msgpack: invalid code=1 decoding string/bytes length

目测是string和int方式的问题,结果发现是

1
2
3
type HealthReply struct {
        Status uint //写成了string,但是赋值的时候写整数
}

golang表单获取不到form参数

type Struct里面的内容是需要大写,我目前粗略是这样

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
type VPNForm struct {
    username string `form:"username"`
}
func AddVPN(c *gin.Context) {
    var vpn VPNForm
    c.Bind(&vpn)
    c.JSON(200, gin.H{
        "message": vpn.username,
    })
func main() {
        r := gin.Default()
        r.GET("/ping", func(c *gin.Context) {
                c.JSON(200, gin.H{
                        "message": "pong",
                })
        })
        r.GET("/vpn",AddVPN)
        r.GET("/geta", GetDataA)
        r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

我这个demo并不能获取,需要把VPN Form里面的username改成Username

改为

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
type VPNForm struct {
    Username string `form:"username"`
}
func AddVPN(c *gin.Context) {
    var vpn VPNForm
    c.Bind(&vpn)
    c.JSON(200, gin.H{
        "message": vpn.Username,
    })
func main() {
        r := gin.Default()
        r.GET("/ping", func(c *gin.Context) {
                c.JSON(200, gin.H{
                        "message": "pong",
                })
        })
        r.GET("/vpn",AddVPN)
        r.GET("/geta", GetDataA)
        r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}