On this instance, you’ll discover ways to write the traditional “Good day World” program. “Good day World” is a straightforward program that shows the phrase Good day World
on the display screen. Historically, that is the primary program written by folks studying to code in a brand new programming language.
Let’s examine how you can write Golang “Good day World” program step-by-step.
In the event you don’t have Golang put in, go to the official Go web site and set up the model applicable on your working system.
Golang “Good day World” program
- Create
hello-world
listingIt’s a good follow that every new undertaking has its personal listing.
- Save the next program as
major.go
within thehello-world
listingbundle major import "fmt" func major() { fmt.Println("Good day World!") }
- Run your program
$ go run hello-world/major.go
Output
Congratulations! You might have simply created your first program in Go 🚀.
How Golang “Good day World” program works
In Go, each program begins with a bundle declaration. A bundle is a set of supply recordsdata used to arrange associated code right into a single unit. We’re declaring a bundle known as major
right here as a result of we might want to declare the major()
perform, which may solely be declared in a bundle named major
.
Subsequent, we now have the bundle import assertion. In our program, the fmt
bundle has been imported and shall be used within the major()
perform to print the “Good day World” textual content to the usual output (your display screen).
The func major()
begins the definition of the major()
perform. The major()
is a particular perform that’s executed first when this system begins. As you already know, the major()
perform in Go ought to at all times be declared within the major
bundle.
fmt.Println("Good day World!")
The major()
calls the Println()
perform of the fmt
bundle. After beginning this system, it prints the handed argument (our textual content “Good day World!”) to the usual output along with a brand new line.