That is the second tutorial in our Golang tutorial collection. Please learn our earlier tutorial Golang Introduction and Set up to find out about what’s Golang and methods to set up Golang.
This tutorial is for Go model 1.13 or later. If you’re utilizing an older model, please go to https://golangbot.com/hello-world/.
There isn’t a higher approach to be taught a programming language than getting our palms soiled with code. Let’s go forward and write our first Go program.
Organising the event atmosphere
Let’s create a listing the place we wish to write our good day world program. Open the terminal and run the next command.
mkdir ~/Paperwork/learngo/
The above command will create a listing named learngo
inside the present person’s Paperwork listing. Be happy to create the listing wherever you need the code to reside.
Making a Go Module
Go modules are used to trace our utility’s dependencies and their variations. Subsequent step is to create a go module named learngo
within the ~/Paperwork/learngo/
folder.
Run go mod init learngo
contained in the ~/Paperwork/learngo/
listing. This can create a file named go.mod
. The contents of the file are supplied under.
module learngo
go 1.17
The primary line module learngo
specifies the module title. The following line go 1.17
signifies that the recordsdata on this module use go model 1.17
We are going to focus on Go modules in additional element once we study packages.
Good day World
Create a file named primary.go
within the learngo
listing utilizing your favorite textual content editor with the next contents.
bundle primary
import "fmt"
func primary() {
fmt.Println("Good day World")
}
It is a conference in Go to call the file that comprises the primary
operate as primary.go
, however different names work as effectively.
Operating a go program
There are a few alternative ways to run a Go program. Let’s take a look at them one after the other.
1. go set up
The primary methodology to run a Go program is utilizing the go set up
command.
Let’s cd
into the learngo
listing we simply created.
cd ~/Paperwork/learngo/
Run the next command subsequent.
go set up
The above command will compile this system and set up(copy) the binary to location ~/go/bin
. The title of the binary would be the title of the go module. In our case, it will likely be named learngo
.
You would possibly encounter the next error if you attempt to set up this system.
go set up: no set up location for listing /dwelling/naveen/Paperwork/learngo outdoors GOPATH
For extra particulars see: 'go assist gopath'
What the above error truly means is, go set up
is unable to discover a location to put in the compiled binary. So let’s go forward and provides it a location. This location is ruled by the GOBIN
atmosphere variable.
export GOBIN=~/go/bin/
The above command specifies that go set up
ought to copy the compiled binary to the trail ~/go/bin/
. That is the traditional location for a Go binary however be happy to alter it to any location you need. Now strive operating go set up
once more and this system ought to compile and run with none issues.
You’ll be able to kind ls -al ~/go/bin/learngo
within the terminal and yow will discover that in actual fact go set up
has positioned the binary within the path ~/go/bin
Now let’s run the compiled binary.
~/go/bin/learngo
The above command will run the learngo
binary and print the next output.
Good day World
Congrats! You’ve efficiently run your first Go Program.
If you wish to keep away from typing your entire path ~/go/bin/learngo
every time you run this system, you may add ~/go/bin/
to your PATH.
export PATH=$PATH:~/go/bin
Now you may simply kind learngo
within the terminal to run this system.
You is perhaps questioning what is going to occur when the learngo
listing comprises muliple go recordsdata as a substitute of simply primary.go
. How will go set up
work on this case? Please maintain on, we’ll focus on these once we study packages and go modules.
2. go construct
The second choice to run this system is utilizing go construct
. go construct
is far just like go set up
besides that it would not set up(copy) the compiled binary to the trail ~/go/bin/
, fairly it creates the binary inside the placement from which go construct
was put in.
Kind the next command within the terminal
cd ~/Paperwork/learngo/
to alter the present listing to learngo
.
After that, enter the next command.
go construct
The above command will create a binary named learngo
within the present listing. ls -al
will reveal {that a} file named learngo
is created.
Kind ./learngo
to run this system. This can even print
Good day World
We’ve efficiently run our first Go program utilizing go construct
too 🙂
3. go run
The third approach to run this system is utilizing go run
command.
Kind the command cd ~/Paperwork/learngo/
within the terminal to alter the present listing to learngo
.
After that, enter the next command.
go run primary.go
After the above command is entered, we are able to see the output
Good day World
One delicate distinction between the go run
and go construct/go set up
instructions is, go run
requires the title of the .go
file as an argument.
Beneath the hood, go run
works a lot just like go construct
. As an alternative of compiling and putting in this system to the present listing, it compiles the file to a brief location and runs the file from that location. If you’re to know the placement the place go run
compiles the file to, please run go run
with the --work
argument.
go run --work primary.go
Operating the above command in my case outputs
WORK=/var/folders/23/vdjz4kt972g5nzr86wzrj9740000gq/T/go-build698353814
Good day World
The WORK
key’s worth specifies the momentary location to which this system might be compiled.
In my case, this system has been compiled to the placement /var/folders/23/vdjz4kt972g5nzr86wzrj9740000gq/T/go-build698353814/b001/exe
. This would possibly range in your case 🙂
4. Go Playground
The ultimate means of operating this system is utilizing the go playground. Though this has restrictions, this methodology is useful once we wish to run easy packages because it makes use of the browser and would not want Go put in in your native :). I’ve created a playground for the good day world program. Click on right here to run this system on-line.
You can too use the go playground to share your supply code with others.
Now that we all know 4 alternative ways to run a program, you is perhaps in a confusion to determine which methodology to make use of. The reply is, it relies upon. I typically use the playground once I wish to do a fast verify of logic or learn the way a normal library operate works. In most different circumstances, I favor go set up
because it offers me an choice to run this system from any listing within the terminal because it compiles all packages to the usual ~/go/bin/
path.
A brief rationalization of the good day world program
Right here is the good day world program we simply wrote
bundle primary
import "fmt"
func primary() {
fmt.Println("Good day World")
}
We are going to focus on briefly what every line of this system does. We are going to dwell deep into every part of this system within the upcoming tutorials.
bundle primary – Each go file should begin with the bundle title
assertion. Packages are used to supply code compartmentalization and reusability. The bundle title primary
is used right here. The principle operate ought to at all times reside in the principle bundle.
import “fmt” – The import assertion is used to import different packages. In our case, fmt
bundle is imported and it will likely be used inside the principle operate to print textual content to the usual output.
func primary() – The func
key phrase marks the start of a operate. The primary
is a particular operate. This system execution begins from the primary
operate. The {
and }
braces point out the beginning and finish of the principle operate.
fmt.Println(“Good day World”) – The Println
operate of the fmt
bundle is used to write down textual content to the usual output. bundle.Perform()
is the syntax to name a operate in a bundle.
The code is accessible for obtain at github.
Now you can transfer on to Golang tutorial half 3: Variables to study variables in Go.
Please put up your suggestions and queries within the feedback part. Thanks.
Subsequent Tutorial – Variables