Tag:golang

  • Golang: usage of built-in container

    Time:2021-9-16

    1. Array Array is a value type [10] Int and [20] int are different types Calling func (arr [10] int) copies the array In go language, data is generally not used directly package main import “fmt” func updateArr(arr *[5]int) { arr[0] = 100 } func updateArrThroughSlice(arr []int) { arr[0] = 100 } func main() { […]

  • Golang gopm get – G – V failed to get the solution of the third-party library

    Time:2021-9-14

    gopm get –g –v golang.org/x/text //The network is blocked [GOPM] ?[36m03-14 07:36:49?[0m [?[31mERROR?[0m] golang.org/x/text: fail to make request: Get https://gopm.io/api/v1/revision?pkgname=golang.org/x/text: dial tcp: look up gopm.io: getaddrinfow: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritativ e server. resolvent cd ~/goPath/src/golang.org/x git […]

  • Perfectly solve the problem of golang go get private warehouse

    Time:2021-9-13

    Solve the problem of golang go get gitlab private warehouse (1.13) 1. Problem description require ( git.xxxxxxx.com/middle/user v0.0.1 ) Go mod tidy import Package Failed Go get git.xxxxxxxx.com/middle/user failed Go build has a checksum process and cannot be compiled 2. Phenomenon analysis Go get does not support warehouses other than code support. And the GIT […]

  • Golang practice – configuration scheme of third-party package as private library

    Time:2021-9-11

    After using go 1.8 normally for a period of time, no exceptions were found. In order to facilitate release and introduce plug-in later, we began to decompose large projects. It involves introducing third-party packages saved in private libraries through vendors. Referring to the materials posted repeatedly on the Internet, it is always unsuccessful, and the […]

  • How does golang call functions in DLL Dynamic Library under windows

    Time:2021-9-9

    Using syscall calls package main import ( “fmt” “syscall” “time” “unsafe” ) const ( MB_OK = 0x00000000 MB_OKCANCEL = 0x00000001 MB_ABORTRETRYIGNORE = 0x00000002 MB_YESNOCANCEL = 0x00000003 MB_YESNO = 0x00000004 MB_RETRYCANCEL = 0x00000005 MB_CANCELTRYCONTINUE = 0x00000006 MB_ICONHAND = 0x00000010 MB_ICONQUESTION = 0x00000020 MB_ICONEXCLAMATION = 0x00000030 MB_ICONASTERISK = 0x00000040 MB_USERICON = 0x00000080 MB_ICONWARNING = MB_ICONEXCLAMATION MB_ICONERROR = […]

  • Operation of compiling golang into DLL file

    Time:2021-9-7

    First write the golang program exportgo.go: package main import “C” import “fmt” //export PrintBye func PrintBye() { fmt.Println(“From DLL: Bye!”) } //export Sum func Sum(a int, b int) int { return a + b; } func main() { // Need a main function to make CGO compile package as C shared library } Compile to […]

  • Detailed explanation of the use of sync.pool in golang

    Time:2021-9-5

    preface We usually use golang to develop and build services in high concurrency scenarios. However, because the built-in GC mechanism of golang will affect the performance of services, golang provides an object reuse mechanism to reduce frequent GC, that is, to build an object pool using sync.pool. Sync.pool introduction First, sync. Pool is a scalable […]

  • Introduction to flexible writing of golang switch statement

    Time:2021-9-3

    Switch is easy to understand. Let’s start with a code and run it Look at your operating system package main import ( “fmt” “runtime” ) func main() { fmt.Print(“Go runs on “) switch os := runtime.GOOS; os { case “darwin”: fmt.Println(“OS X.”) case “linux”: fmt.Println(“Linux.”) default: fmt.Printf(“%s”, os) } } Runtine runtime obtains the current […]

  • Several definitions and operation sequence of init method in golang package

    Time:2021-9-2

    Before I knew this problem, I searched the Internet and found two completely opposite results, so I planned to test this problem myself. Firstly, the conclusion is given: In the same package, the init method can be defined in multiple files The init method can be defined repeatedly in the same go file In the […]

  • Solve the problem of golang about global variables

    Time:2021-8-27

    Soon after learning golang, I met a pit when defining global variables. Write a small example to enhance memory. Wrong version var p int func main() { p, err := test(4) if err != nil { log.Fatal(err) } } func test(i int) (int, error) { return i + 1, nil } P declared and not […]

  • Golang’s case of realizing the small function of HTTP file upload

    Time:2021-8-25

    Look at the code~ package main import ( “fmt” “io” “net/http” “os” ) func main() { http.HandleFunc(“/”, index) http.HandleFunc(“/upload”, upload) http.ListenAndServe(“:1789”, nil) } func upload(w http.ResponseWriter, r *http.Request) { r.ParseMultipartForm(32 << 20) file, handler, err := r.FormFile(“uploadfile”) if err != nil { fmt.Println(err) return } defer file.Close() f, err := os.OpenFile(handler.Filename, os.O_WRONLY|os.O_CREATE, 0666) if err […]

  • How does golang implement HTTP proxy and reverse proxy

    Time:2021-8-24

    The core function of the agent can be summarized in one sentence: accept the client’s request, forward it to the back-end server, and return it to the client after obtaining the response. There are many functions of proxy. In fact, the whole Internet is full of proxy servers. If all HTTP accesses are performed directly […]