Home  >  Article  >  Backend Development  >  Do you know the two versions of Hello world in Go language?

Do you know the two versions of Hello world in Go language?

angryTom
angryTomforward
2019-11-28 14:20:042433browse

When learning a programming language, in addition to the basic syntax, it is more important to understand how to use it in actual projects. In this article, I will work with you to install the Go language, configure the environment, install the IDE, and develop a Hello World program from scratch. I will write two versions of this Hello World version: GOPATH and Go Module versions.

Do you know the two versions of Hello world in Go language?

Q:
Why are there two versions of this tutorial?
A:
Most Go language Hello World on the Internet only briefly introduces the GOPATH version. However, starting from version 1.11 of Go, it is no longer recommended to use GOPATH to build applications. In other words, GOPATH is considered obsolete and a wrong approach.
The correct approach is to use Go Module. Therefore, it is necessary to tell beginners this information in the tutorial and guide them to use the recommended best practice Go Module.
Perhaps this is also the difference between this Hello World tutorial and most tutorials on the Internet. It will start from the actual use of developers. Introduce the development history and best practices of Go language.

Installing Go

To develop in Go language, the first step is to install Go.

Step one: Enter the official website of Go language https://golang.org. Click "Download Go". Yes, it’s that silly groundhog ^_^

Do you know the two versions of Hello world in Go language?
Step 2: Download the corresponding software package according to the operating system and install it.

Golang supports mainstream operating systems, such as Windows, MacOS and Linux.
In this series of tutorials, I will use the MacOS operating system, so I choose to download "Apple macOS". Everyone should download according to their own operating system.
Do you know the two versions of Hello world in Go language?
Step 3: Install Go.

Installing Go under MacOS is very simple. It is a standard DMG file installation. Just "Next".

Step 4: Confirm whether Go is installed successfully.

Run the command "go version", and something similar to "go version go1.12.9 darwin/amd64" will be output. Please make sure that no errors occur with the package.

Configuration environment

Similar to other development languages, after installing Go, you need to configure the development environment accordingly. For example, in Java, JAVA_HOME, MAVEN, etc. need to be configured.
The environment variables related to Go development are as follows:

● GOROOT: GOROOT is the installation directory of Go.

●GOPATH: GOPATH saves the go project code.

GOROOT

GOROOT is the installation path of Go. Installing Go on Mac will automatically configure GOROOT, and the path is /usr/local/go. GOROOT does not need to be modified in most cases. The following is the content of the GOROOT directory (some irrelevant information has been omitted):

tree -L 2 /usr/local/go

./
├── bin
│   ├── go
│   └── gofmt
├── doc
│   ├── articles
│   └── docs.html
├── src
│   ├── errors
│   ├── fmt
│   ├── log
│   └── os

You can see that there are bin, doc and src directories under GOROOT. There are familiar go and gofmt tools in the bin directory. It can be considered that GOOROOT is similar to the JDK directory in Java.

GOPATH

GOPATH is the working directory during development. Used for:

● Save the compiled binary file.

● The go get and go install commands will download the go code to GOPATH.

● Search path when importing packages.

Special attention needs to be paid to GOPATH. Before version 1.11 of Go, GOPATH was required, and all Go project code must be saved in the GOPATH directory. After Go version 1.11, GO officially introduced Go Module. Projects managed using Go Module can be placed outside the GOPATH directory.

When using GOPATH, GO will search for packages in the following directories:

1. GOROOT/src: This directory saves the Go standard library code.

2. GOPATH/src: This directory saves the application's own code and the code of third-party dependencies.

Assume that the following package is introduced in the program:

import "github.com/tom/hello-go/foo/bar"

The first step: Go will first search in the scr directory of GOROOT. Obviously it is not a standard library package and was not found.

Step 2: Continue to search in the src directory of GOPATH. To be precise, it is the directory GOPATH/src/github.com/tom/hello-go/foo/bar. If the directory does not exist, an error will be reported that the package cannot be found. When using GOPATH to manage projects, you need to properly save and organize Go code according to GO's specifications for finding packages.

Go的这个“将所有代码都放置在GOPATH中”的设计,的确是和其他主流语言很不一样。不管Go官方是出于什么考虑,这个设计在实际使用中,的确给开发者造成了很大的不便和理解上的困难。甚至直接劝退了很多Go的初学者。

万幸的是,Go Module正式发布了。Go Module的发布解决了困扰Go语言长达十年的代码组织,依赖管理问题。

说明:关于GOPATH和Go Module的历史渊源,详细使用,会在另一篇进行说明。本文还是聚焦在Hello World入门。

另外,由于某些原因,Go的某些托管在Google仓库中的代码在国内是无法访问的。如果使用Go Module,我们可以设置GOPROXY,让Go从GOPROXYDo you know the two versions of Hello world in Go language?代码,速度更快。国内用户可以设置GOPROXY为https://goproxy.cn,使用如下命令来设置GOPROXY:

export GOPROXY=https://goproxy.cn

关于环境配置,总结下来就是:

● 如果使用Go Module(推荐的),设置export GOPROXY=https://goproxy.cn。

● 如果使用GOPATH(遗留的,被废弃的),需要设置GOPATH到本地的某个目录。

安装IDE

目前比较常用的IDE有:

● Visual Studio Code

● GoLand

Visual Studio Code是微软开发的一款开源的,轻量级的文本编辑器。通过安装Go插件,可以用于Go语言的开发。GoLand是JetBrains公司开发的,专业的Go语言开发IDE。

推荐使用GoLand。很多人都说Visual Studio Code更轻量级,但作为一款每天都要重度使用的,需要靠它吃饭的工具,我们需要的是功能全面。尤其是当你需要完备的调试,需要强大的IDE智能辅助功能时,相信你会选择更专业的GoLand。

GoLand的官方主页为https://www.jetbrains.com/go/,点击“Download”即可下载

注意
GoLand使用的是和IntelliJ IDEA一样的框架,是用Java语言开发的。你需要安装Java环境才可以运行GoLand哦。

GoLand是收费软件,只有30天的试用期。试用期结束后,需要购买授权。当然在天朝,我们都习惯不花钱用软件。大家可以自行百度或google搜索一下。

Hello World(GOPATH版)

第一步:设置GOPATH

首先设置GOPATH,假设GOPATH设置为$HOME/worspace/go。

第二步:创建子目录

进入$HOME/workspace/go目录。新建子目录src。然后再src中新建子目录hello。在hello目录,新建一个hello-world.go文件:

目录结构应该如下所示:

$HOME
  workspace
    go
      src
        hello
          hello-world.go

第三步:创建hello-world.go文件:

package main
import "fmt"
func main() {
    fmt.Println("hello world")
}

第四步:执行go build

在$HOME/workspace/go目录执行命令:

go build

会生成一个可执行二进制文件:hello。如果是Windows系统,会生成hello.exe文件。
第五步:运行hello文件:

./hello

hello world

输出“hello world”。

Hello World(GO Module版)

第一步:创建项目的根目录

任意创建一个目录(可以不在GOPATH中),假设是$HOME/tmp/hello。

第二步:初始化Go模块

执行命令:

go mod init github.com/vangoleo/hello

该命令会将hello目录初始化为一个Go module,并生成一个$HOME/tmp/hello/go.mod文件。内容如下:

module github.com/vangoleo/hello

go 1.12

第三步:编写hello.go文件

编写文件$HOME/tmp/hello/hello.go:

package main

import (
    "fmt"
    "rsc.io/quote"
)

func main() {
    fmt.Println(quote.Hello())
}

第四步:编辑go.mod文件:

在实际项目中,都会使用到第三方库。可以在Go Module中添加项目的依赖。本例中,我们会添加一个quote依赖,该依赖会打印当前语言的“Hello World”,比如,如果是中文环境,会打印“你好,世界”。

编辑go.mod文件,添加quote依赖:

module github.com/vangoleo/hello

go 1.12

require rsc.io/quote v1.5.2

第五步:执行go build

执行go build,会生成可执行文件$HOME/tmp/hello/hello。

第六步:执行hello文件

执行hello文件,输出“你好,世界”。

推荐:go语言教程  

The above is the detailed content of Do you know the two versions of Hello world in Go language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete