Category:Golang

  • [series] go – Chan channel

    Time:2019-12-15

    Catalog Summary Statement Chan Write to Chan Read Chan Close Chan Example Recommended reading Summary When we originally shared the basic grammar, we haven’t shared the Chan channel yet. This time, we need to add it. Chan can be understood as a queue, following the first in, first out rule. Before we talk about Chan, […]

  • Detailed explanation of golang import import package syntax

    Time:2019-12-15

    Import syntax of package When writing go code, you often use the command import to import a package, as follows: import( “fmt” ) Then you can call it in the code in the following ways: FMT. Println (“I love Tian’anmen, Beijing”) FMT is the standard library of go. In fact, it goes to goroot to […]

  • Golang reading and writing files

    Time:2019-12-14

    read file func ReadFile_v1(filename string) { var ( err error content []byte ) fileObj,err := os.Open(filename) if err != nil { fmt.Println(“os open error:”,err) return } defer fileObj.Close() content,err = ioutil.ReadAll(fileObj) if err != nil { fmt.Println(“ioutil.ReadAll error:”,err) return } fmt.Println(string(content)) } //There’s another way func Readfile_v2(filename string) { var ( err error content []byte […]

  • Go download non-standard library packages (some of them are blocked) to local users

    Time:2019-12-14

    As follows: import ( “golang.org/x/net/html” ) When using this package, it will fail to import because it is not in the go standard library, but because the domain name is walled, you can download this package over the wall and put it into the “golang.org/x/net” directory under your gopath path path Specific steps Download the […]

  • The method of introducing custom package into golang

    Time:2019-12-13

    After reading the basic syntax of golang, in order to modularize programming, I tried the package management of golang. As a result, I stepped into several holes. Let’s summarize. I. set $gopath environment variable Different from C or PHP, golang does not automatically find files in the current path. You must first add your own […]

  • Go upload picture wechat server < temporary material

    Time:2019-12-13

    type WxImage struct { Type string `json:”type”` MediaId string `json:”media_id”` CreatedAt int `json:”created_at”` } func UpImage(token, filename string) string { //Open file fh, err := os.Open(filename) if err != nil { logs.Info(“error opening file”) return “” } defer fh.Close() bodyBuf := &bytes.Buffer{} bodyWriter := multipart.NewWriter(bodyBuf) fileWriter, err := bodyWriter.CreateFormFile(“image”, filepath.Base(filename)) if err != nil { […]

  • Golang uses JSON format to add, delete, query and modify

    Time:2019-12-12

    Needs and ideas In a general small project or a small software, such as a client, data persistence may be required. However, it is not appropriate to use a general database (MySQL). SQLite3 is a better embedded method, but SQLite3 Library in go language is C language, CGO does not support cross platform compilation. It […]

  • Go time / timestamp operations (summary)

    Time:2019-12-12

    basic operation 01: get current time dateTime := time.Now() fmt.Println(dateTime) 02: get month day hour minute second nanosecond Year: = time. Now(). Year() // year fmt.Println(year) Month: = time. Now(). Month() // month fmt.Println(month) Day: = time. Now(). Day() // day fmt.Println(day) Hour: = time. Now(). Hour() // hour fmt.Println(hour) Minute: = time. Now(). Minute() […]

  • Go basic data type

    Time:2019-12-11

    Go basic grammar package main import “fmt” func main(){ fmt.Println(“Hello world”) } Note: ‘{‘ to the left of curly braces cannot start another line, otherwise compilation error Else in an if… Else statement must be on the same line as “}” of if, otherwise compilation error The package name must be declared in the first […]

  • A detailed explanation of formatting time in golang Gorm

    Time:2019-12-11

    Preface Recently, I encountered a problem when I was developing a project, Gorm can automatically help you maintain the created at, updated at, and deleted at key time fields. But its principle and disadvantages need to be understood. 1. Usage Control the time format by customizing the structure of a Localtime package utils import ( […]

  • Add, delete, modify and query driven by the official database of go mongodb

    Time:2019-12-10

    package main import ( “context” “fmt” “log” “go.mongodb.org/mongo-driver/bson” “go.mongodb.org/mongo-driver/mongo” “go.mongodb.org/mongo-driver/mongo/options” ) // Trainer type is used for later type Trainer struct { Name string Age int City string } func main() { // Set client options clientOptions := options.Client().ApplyURI(“mongodb://localhost:27017”) // Connect to MongoDB client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { log.Fatal(err) } […]

  • Solutions to the problem of packet sticking and unpacking in golang TCP

    Time:2019-12-10

    What is sticking problem Recently, I used golang to write the socket layer, and found that sometimes the receiver would read multiple packets at a time. So by looking up the data, we found that this is the legendary TCP packet sticking problem. Write code to reproduce this problem: Server / main.go func main() { […]