Software Developer
Understanding hello world program in go
April 16, 2018
In this post, you will learn about five important question that you might have while writting the hello world program in Go
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
You must have Go installed in your local machine, if you haven't you can install from here, After this, in your terminal you can type go
to check the list of commands available.
To run your first program on go, you have to go inside project directory and just type go run <filename>
, Yes, it's that simple.
package main
mean ?A package is a collection of common source code file, there are two types of packages - Executable
and Reusable
, Executable packages are those which we generally write to execute a program. Reusable packages are helpers programs, they are the best place to write reusable logic.
Here main
is the package name that determines whether we are writing Executable or Reusable package. Here package main
is a executable package. It must always have a func called main
.
import "fmt"
mean ?It means, give my package main
access to all the code/functionality inside the package fmt
, It is the name of standard library package that is included in the golang by default, It is kind of shorten form of format. fmt library is used to print out stuffs to terminal, just to give better sense of debugging. Official docs of fmt
func
thing ?func
is the short form for the function. In our program we have func main()
, where main is the function name followed by parenthesis where you can pass arguments.
helloworld.go
file organised ?At the very top we are going to have a package declaration. ex - package main
Then we will import other packages that we need in our program. ex - import "fmt"
Then we declare functions, tell golang to do things ex - func main(){}