Tag:golang

  • Golang cast and type assertion

    Time:2022-6-9

    catalogue 1. forced type conversion 2. type assertion 1. forced type conversion Golang is a strongly typed language, but unlike c++ and Java, there is no implicit type conversion in go. The only type conversion in go is forced type conversion and assertion In Java: ? 1 2 3 4 5 public static void main(String[] […]

  • Solve the problem of null value in golang JSON parsing

    Time:2022-6-9

    I use the beego framework to parse the requested JSON and save the value in the structure ——————–1——————— request := UpdateCommentRequestData{} req := common.Request{Data: request} err := json.Unmarshal(controller.Ctx.Input.RequestBody, &req) —————————————— The structure of updatecommentrequestdata is as follows type UpdateCommentRequestData struct { Id []string `json:”id”` } common. The structure of the request is as follows type […]

  • Solve the problem of too many \ when serializing strings in golang JSON

    Time:2022-6-8

    When we provide API interfaces and return responses, we often need to use the following data structures type Response struct { Code int `json:”code”` Msg string `json:”msg”` Data interface{} `json:”data”` } The API interface returns a status code, status information, and specific values. However, the specific value may vary according to the different interfaces. In […]

  • Golang:json deserialized [] and nil operations

    Time:2022-6-7

    I won’t say any more nonsense. Let’s just look at the code~ package main import ( “encoding/json” “log” ) type JS struct { A []string } func main() { v1 := &JS{} v2 := &JS{A: []string{}} o1, err := json.Marshal(&v1) log.Println(string(o1), err) o2, err2 := json.Marshal(&v2) log.Println(string(o2), err2) } result 2019/01/07 18:13:26 {“A”:null} <nil> 2019/01/07 […]

  • Golang judge net Conn whether the operation has been closed

    Time:2022-6-6

    In a multithreaded thread, a thread is used to process a connection. How to judge whether the connection has been closed? Try it. If the connection is interrupted, this kind of net Operror, this can determine whether to disconnect But I’m not sure if there’s a better way? If so, I hope my friends will […]

  • Implementation example of golang crawler and regular expression

    Time:2022-6-5

    catalogue character Quantity limit other Reptile Supplement: regular expression plus golang crawler crawling classic case watercress top250 Recently, I learned go, crawled website data and used regular expressions to make a summary; The regular expression in go uses re2 syntax (we don’t know what it is); character . — match any character e.g: abc Results: […]

  • Golang obtain network card information

    Time:2022-6-5

    I won’t say any more nonsense. Let’s just look at the code~ package main import ( “fmt” “os/exec” “strings” “github.com/safchain/ethtool” ) func main() { baseNicPath := “/sys/class/net/” cmd := exec.Command(“ls”, baseNicPath) buf, err := cmd.Output() if err != nil { //fmt.Println(“Error:”, err) return } output := string(buf) for _, device := range strings.Split(output, “\n”) { […]

  • Explain the golang functional options mode

    Time:2022-6-4

    overview Recently, I saw a good piece of code when reading the source code, but I didn’t quite understand why it was written like this at that time.Let’s first look at the source code: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 […]

  • Summary of golang dynamic call method

    Time:2022-6-3

    main.go ? 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 28 29 30 31 32 package main   import (  “lenu/call”  “reflect” )     type FuncCollection map[string]reflect.Value   func main() {  _, _ = Callfunc […]

  • Implementation of random sorting of golang array

    Time:2022-6-2

    catalogue preface The specific implementation steps are as follows 1. import and storage 2. assemble data and sort (scheme I) 3. assemble data and sort (scheme II) summary preface At present, we have received a demand for recommended data. We need to randomly sort the data obtained in the database and return it to the […]

  • Sample code for golang to implement array segmentation

    Time:2022-6-1

    Requirements: given an array and a positive integer, it is required to divide the array into multiple arrays of positive integer size. If not enough, the last array will be divided into all remaining elements. Example 1: Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], positive integer: 2Expected results: [[1, 2], [3, […]

  • The usage of the omitempty of JSON in golang

    Time:2022-6-1

    I won’t say any more nonsense. Let’s just look at the code~ package main import ( “encoding/json” “fmt” ) type Project struct { Name string `json:”name”` Url string `json:”url”` Docs string `json:”docs,omitempty”` } func main() { p1 := Project{ Name:”hello name”, Url:”https://blog.csdn.net/qq_30505673″, } data, err := json.Marshal(p1) if err != nil { panic(err) } //Docs […]