Writing your first Go Lang program
Author: Sumit Dhiman
Tags: GO Lang Code Hello World BeginnersFebruary 14, 2023
Go is an open-source, statically typed programming language designed to build scalable, secure, easy-to-use systems. Given that Go Lang was created by Google, Google Go is another moniker for it. It was developed to fill the gap between Java and C++ to provide code that is easy to understand, maintain, and execute without degrading. Go is a compiled language with features that are comparable to those of C and C++ and share the same initial main function.
Installing Go:
- For windows users: https://go.dev/dl/
- For Linux users:
- Arch Linux:
sudo pacman -S go
- Debian:
sudo apt install go
- Arch Linux:
Let’s start creating our first Hello world code:
-
First of all create a folder using following command:
mkdir <Your folder>
-
Now
cd
into your folder and create a file namedmain.go
.
cd <Your folder>
touch main.go
-
Now we need to initialize our go module in this folder.
go mod init <your module name>
-
We have initiated our module. So, we can start editing our
main.go
file. -
Here, you can see written
package main
, this determine its package name so that we can import go function from one directory to another. -
fmt
package stands forformat
. It is used to print our data by usingPrintln
function
package main
import "fmt"
func main(){
fmt.Println("Hello!, World")
}
- run and build the program using command below:
$ go run .
$ go build