This is a creation in Article, where the information may have evolved or changed.
Go language Getting Started tutorial-Hello World
This is the second tutorial for getting started with the Go Language tutorial. Before you go to the second tutorial, read the first section introduction and installation to know how to install Golang.
There is no better way to learn the programming language faster than our hand-tapping code. So let's hurry up and write our first program.
I personally recommend using Visual Studio code as the Go Language editor, which supports auto-completion, code styling (such as highlighting), and many other features.
Set Workspace
Before we start coding, we have to set the workspace of the Go language.
In the Mac, Linux environment, we set the workspace to $HOME/go. We can use mkdir ~/go
the command to create the directory.
In the Windows environment, we should set the workspace to C:\Users\YourName\go
, we can use mkdir C:\Users\YourName\go
the command to create the directory.
By setting the GOPATH environment variable, we can use a different directory as the workspace, but now we simply follow the above settings.
All source code files should be placed in a directory called src
, then we go
create directories in the directory src
.
Each go project should src
have a corresponding subdirectory in the directory. Let's start by creating a Hello new directory to hold hello world
the code for the project.
After we have created the catalog, the directory structure looks like this
go src hello
Save the following code and name helloworld.go
it in the folder that we just created hello
.
package mainimport "fmt"func main() { fmt.Println("Hello World")}
After creating the program file above, our directory structure looks like this
go src hello helloworld.go
Run Go Program
Here are a set of two go
ways to run a program. Let's take a look.
1) at the command prompt, enter go run workspacepath/src/hello/helloworld.go
The above code to be able to function properly, the premise is that the workspace (workspace) has been set.
windows: C:/Users/YourName/go
,linux or Mac: $HOME/go
Next you will see the output in the terminal Hello World
.
2) Use go install hello
the compile and install Hello Project and then use the workspacepath/bin/hello
run program
The above code to be able to function properly, the premise is that the workspace (workspace) has been set.
windows: C:/Users/YourName/go
,linux or Mac: $HOME/go
When we use the go install hello
command, we go tool
will search hello
for this package (known as the hello
package, which we will refer to in detail in the package section) at workspace. Then a binary file is generated in the bin directory that hello
is called (in the windows
Yes hello.exe
). Then our directory structure is like this.
go bin hello src hello helloworld.go
A simple explanation of the Hello World program
Here is the code of the program we just wrote
package main //1import "fmt" //2func main() { //3 fmt.Println("Hello World") //4}
Package main - each Go program must start with the packages name . The package is designed primarily for code isolation and code reusability. The package name inside this program is called Main.
Import "FMT" -imports the FMT package for output text in the main function to the standard output device
The func main () -main function is a special function. The application starts execution from the main function. The main function must be placed in the main package . The {and} indicate the start and end of the main function.
FMT. Println ("Hello World") -Use the fmt
package to Println
output text to a standard output device