Category:Golang

  • How to bind the host in the go code?

    Time:2021-10-27

    Within the company, for security, there may be domain names that can only be accessed by binding host. If accessed through a browser, we will modify the hosts file of the machine; However, if we want to access such domain names through programs, do we still have to bind host? Of course, the answer is […]

  • Go object-oriented thought: encapsulation, inheritance and polymorphism

    Time:2021-10-27

    How do go implement encapsulation, inheritance, and polymorphism Encapsulation: go realizes the public and private of class methods and attributes through the case of the initial letter Inheritance: use the embedded method to inherit the structurestructCombine Polymorphism: Usinginterfacerealization Encapsulation: go simulates classes through the method of structs package objects //Simulate classes by means of structs […]

  • The golang application packages the docker image and runs it

    Time:2021-10-27

    Use the gin framework to build a most basic web service to illustrate the whole process. Write program code main.go package main import “github.com/gin-gonic/gin” func main() { r := gin.Default() r.GET(“/ping”, func(c *gin.Context) { c.JSON(200, gin.H{ “message”: “pong”, }) }) r.Run(“0.0.0.0:8080”) // listen and serve on 0.0.0.0:8080 (for windows “localhost:8080”) } Note that the host […]

  • Golang deletes files and recursively deletes empty directories

    Time:2021-10-27

    Purpose: After deleting the file, if the directory is empty, recursively delete the empty directory. Svr.datadir refers to the outermost layer of the directory. You can judge this layer when deleting the directory recursively. eg:/Users/danny/data func deleteFileOnDisk(localPath string) { log.Debugf(“remove file: %s”, localPath) if err := os.Remove(localPath); err != nil { log.Error(err) } dirsList := […]

  • Reload go application in real time

    Time:2021-10-26

    brief introduction 1. You can implement live reload of go code2. Based onfreshNo maintenance, no developmentair characteristic Colorful log output Customize build or binary command Support excluding subdirectories Allow watching new directories after Air started Better building process install go get -u github.com/cosmtrek/air use air Output: 1. Because MySQL service is not enabled locally, there […]

  • Do you know the implementation scheme of set in go?

    Time:2021-10-26

    GoOur design is a simple philosophy, It discards some bloated functions and modules of other languages to reduce the learning threshold of programmers and reduce the mental burden in use. In this paper, we discuss the missing data structure in go:Set, and its best implementation. Set semantics and implementation scheme Set is a common data […]

  • Go language introduction series (IV) the use of map

    Time:2021-10-26

    Previous articles in this series: Go language introduction series (I) installation and use of go Basic grammar summary of go language introduction series (2) Go language introduction series (3) arrays and slices 1. Declaration A map is a map that allows you to(key)Map to value(value), in the format:map[keyType]valueType。 Declare a map and initialize it, usingkey:valueTo […]

  • How does golang delete source path information in binary files

    Time:2021-10-26

    method Go v1.13 go build adds the – trimpath parameter, which is not as troublesome as before. ➜ awesomeProject CGO_ENABLED=0 go build -v -a -ldflags=”-w -s” -trimpath \ -o ./hello_word hello_word.go ➜ awesomeProject strings hello_word|grep src ➜ awesomeProject #Before ➜ awesomeProject go tool objdump hello_word TEXT go.buildid(SB) ….. TEXT main.main(SB) /Users/xxxx/go/src/awesomeProject/hello_word.go hello_word.go:3 0x104e580 65488b0c2530000000 MOVQ […]

  • How does go language manipulate Kafka to ensure no message loss

    Time:2021-10-25

    background At present, some Internet companies will use message queuing for core business. Because it is a core business, it is sensitive to the final consistency of data. If there is data loss in the middle, it will lead to user complaints, and the performance at the end of the year will become 325. Before […]

  • A thorough understanding of the hystrix go source code

    Time:2021-10-25

    Series articles: Usage and principle of hystrix go A thorough understanding of the hystrix go source code Opening The last article mainly introducedhystrix-goIn this article, let’s comprehensively analyze the source code. This article is very long. Please read it patiently. In addition, because the direct release of the source code affects the mobile phone reading […]

  • go defer panic recover

    Time:2021-10-25

    Golang does not have the exception handling structure of try… Catch… But uses defer, panic and recover to handle exceptions. 1. Defer, which literally means delay and postponement, is to delay the method behind defer until the method wrapping the method returns for execution.       From the above code, we can see that defer […]

  • Go run main.go keeps prompting that the package solution cannot be found

    Time:2021-10-25

    As follows: ./src/github.com/sirupsen/logrus/terminal_check_notappengine.go:9:2: cannot find package “golang.org/x/crypto/ssh/terminal” in any of The corresponding package has been downloaded from githup in the corresponding path https://github.com/golang/crypto But it still prompts this error At that time, the project was running in the ~ / go directory, and the imported packages could not call each other When the project is […]