In this shot, we will learn how to print Hello, World
in Golang.
Hello, World
is the primary program in every programming language.
First, we must understand some basic concepts in Golang:
.go
file extension.import
keyword.Before diving into code, we should understand the use of packages in Golang:
main
package that tells the compiler that this program is executable.//provide the package mainpackage main//import the packagesimport("fmt")//Program execution starts herefunc main(){//Print statementfmt.Println("Hello, World")}
In the code snippet above:
Line 2: We declare the package name. We declare it as a main
package, and the compiler then understands that this program is executable and not a library code.
Line 5: We import the format
package, which helps format input and output.
Line 10: We use func
to declare a function. Here, we declare and define the main
function. The program execution starts from the main
function in Golang.
Line 13: We use the Println()
function provided by the fmt
package to print the statement, Hello, World
.