GOLANG, as a statically typed programming language, has developed rapidly in the development field in recent years and has many advantages, such as efficiency, simplicity, security, etc., so it has attracted more and more attention. This article will introduce how to open and use GOLANG.
1. Preparation
Before starting to use GOLANG, you need to download and install GOLANG first. Download address: https://golang.org/dl/, select the installation package corresponding to the system version to download. In addition, you also need to configure the environment variables of GOLANG, add GOROOT and GOPATH variables to the system environment variables, and set their values to the installation path of GOLANG and the working directory of the current project respectively.
2. Writing a Hello World program
GOLANG's official website provides many sample programs, including the classic "Hello World" program. The steps to write this program are as follows:
Write the following code:
package main import "fmt" func main() { fmt.Println("Hello, World!") }
3. Run the program
Run the GOLANG program There are two main ways: command line running and IDE (integrated development environment) running.
Command line operation:
Open the command line interface, cd into the folder where the Hello World program is located, and execute the following commands to compile and run:
go build main.go ./main
4. Using third-party libraries
In GOLANG, many extended functions need to be implemented through third-party libraries. Before using the third-party library, you need to download it locally through the go get command.
Taking the gin framework as an example, the following are the steps:
Execute the following command to download:
go get -u github.com/gin-gonic/gin
Create a new file, name it server.go, and write the code in it:
package main import ( "github.com/gin-gonic/gin" "net/http" ) func main() { router := gin.Default() router.GET("/", func(c *gin.Context) { c.String(http.StatusOK, "Hello, World!") }) router.Run(":8080") }
Save and exit, run the following command to start the server:
go run server.go
Summary:
Through the introduction of this article, we can understand the basic use of GOLANG and the use of third-party libraries. Although GOLANG has a steep learning curve, its efficiency, simplicity, security and other features still make it one of the preferred languages for many developers.
The above is the detailed content of Let's talk about how to open and use GOLANG. For more information, please follow other related articles on the PHP Chinese website!