Golang/Gin getting started

Failed to download golang toolkit

1
2
3
4
5
$ go env -w GOPROXY=https://goproxy.cn
$ go get -v golang.org/x/tools/gopls
go: downloading golang.org/x/tools/gopls v0.7.1
go: downloading golang.org/x/tools v0.1.5
go: downloading golang.org/x/tools v0.1.6-0.20....

cannot initialize 1 variables with 2

1
input, _ := strconv.ParseInt(scanner.Text(), 10, 64)

here the strconv will return a set of two values, one for result and one for error exception, so we add the _ to catch the exception msg

difference between := and =

https://blog.csdn.net/hxpjava1/article/details/79865133

check and update go.mod for all dependency

1
$ go mod tidy

this cmd will automatically download the missing dependency and update it to go.mod.

context

https://zhuanlan.zhihu.com/p/110085652

buffered channel

Buffered channels allows to accept a limited number of values without a corresponding receiver for those values.
Buffered channel are blocked only when the buffer is full. Similarly receiving from a buffered channel are blocked only when the buffer will be empty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
c1 := make(chan string, 1)  // Buffered
c2 := make(chan string) // Non-buffered

select {
case res := <-c1:
fmt.Println(res)
case <-time.After(1 * time.Second):
fmt.Println("timeout 1")
}
// res cannot receive any value from channel c1 as it is blocked due to the empty buffer

go func() {
time.Sleep(2 * time.Second)
c1 <- "result 1"
}()
// after the above goroutine is done, channel c1 is blocked as the buffer is full
// no more incoming message is acceptable until the buffer is released.
// This is a common pattern to prevent goroutine leaks in case the channel is never read.

net/rpc usage

https://blog.csdn.net/raoxiaoya/article/details/109473904

mongo db

  1. collection: https://docs.mongodb.com/manual/core/databases-and-collections/
    Collections are analogous to tables in relational databases

package xxx/middleware is not in GOROOT (C:\Program Files\Go\src\xxx\middleware)

not sure about how to fix it, but I just no longer saw same error happen in new project any more …

introduce package under same project

1
2
3
4
5
project-1
- /server
- server.go
- func NewServer
- main.go

Here I want to introduce func NewServer into main.go

  1. define module for project-1
    1
    $ go mod init project-1   # under pwd /project-1
  2. name package name in server.go and make sure the func name is start with upper class letter
    1
    2
    3
    4
    5
    package server
    ...
    func NewServer() {
    ...
    }
    https://segmentfault.com/q/1010000041390281
    If your func is not started with upper class letter, go kit for vscode cannot detect the func and will show error like:
    1
    func not exported by package
    Auto saving will remove your import package as it is considered not used.
  3. import to main.go
    1
    2
    3
    4
    5
    import(
    "project-1/server"
    )
    ...
    s := server.NewServer()
    btw, if you are introducing struct or func inside the same package, there’s no need to import, go ahead and use it directly.

gin.Context vs http.Request & http.ResponseWriter

gin.Context is encapsulated as the combination of http.Request & http.ResponseWriter
https://juejin.cn/post/6844903830669262855

rsa encryt

https://zhuanlan.zhihu.com/p/384595092

gin.H

= map[string]interface{}
https://segmentfault.com/q/1010000017476060/a-1020000017486404

add, delete for go Array

Mainly use Slice, see https://blog.csdn.net/youngwhz1/article/details/83026263