Golang is a programming language that popularly used to build web applications. In this article, I'm going to give a brief introduction to Golang (aka "Go")
Installation
To install Golang, please follow the instructions below.
Download the latest version of Golang from Golang website.
Follow the installation instructions as per your OS.
Set environment variables- For e.g. on Mac, I would run the following commands:
-
export GOPATH=$HOME/go
- This is the path where I will store my Golang projects. This is used as default by the Go compiler to find the packages you create. -
export GOROOT=$HOME/go
- This is the directory where you will find thego
binary. -
export PATH=$PATH:$GOROOT/bin
- This adds the 'go' executable to the PATH variable so that you can run it from anywhere.
Let's say Hello to the world
Let's look at a simple traditional Hello world program.
Create a new directory in your home directory and name it hello
.
Create a new file in the hello
directory and name it main.go
.
In the main.go
file, write the following code:
package main // Package declaration
import "fmt" // Import statement
func main() { // Main function
fmt.Println("Hello, world!")
}
Let's run this file in a terminal.
Run the following command:
go run main.go
Hello, world!
Now, let's look at the code in a more detailed way.
- The first line of the file is the package declaration. If you are familiar with Java, this is the same as the package declaration in Java. This will be helpful when you want to import this file into another Go file.
- Next we have the import statement. It includes the standard package
fmt
(Format). This package is used here to print the output. It can also be used to read input from the user. There can be many use cases forfmt
but let's not dig deep right now. - The
main()
function is the entry point of the program. This is where the program execution begins (again, this is the same as the main method in Java but more concise).
Note: Go provides a simpler print/println
function too. However, it is not recommended to use this function. fmt
is the preferred way because it is more flexible and can be used for many use cases.
Let's try a function call
Let's change the output of the program to print the name of the user.
For this, we will use the fmt
package.
Let's add the following code to the main.go
file:
package main
import "fmt"
// function that reads name from console and returns it
func getName() string {
var name string
fmt.Println("Enter your name: ")
fmt.Scanln(&name)
return name
}
// a main function which calls the getName() method and prints Hello with the name
func main() {
name := getName()
fmt.Println("Hello, %s!", name)
}
Let's run this file in a terminal.
Run the following command:
go run main.go
Enter your name:
John
Hello, John!
Let's look at the getName() function.
- The first line of the function is the function declaration. It has two parts: the return type
string
and the function namegetName
. - Next we have the variable declaration. This is where we declare the variable
name
of typestring
. We will cover more on variables in the next article. - Notice the return statement. This is where the value of the variable
name
is returned to the main function.
Another point to notice is the :=
operator used in the main function. This is a shorthand for declaring and initializing a variable. Here, we are declaring the variable name
and initializing it with the value returned by the getName()
function. We do not need to write the var
keyword or the type of the variable.
This should give you a tiny idea of how Go works. In the next article, I will cover the basics of using variables and functions in Go. Stay tuned.
Thanks for reading. If you want to connect with me, you can find me on Twitter @abh1navv.
Top comments (2)
I just got started on Go a few days ago.
For those who have known C, Go is like a modern C, with the addition of such things as implicit Interface which can be confusing for those who have known C++, Java, PHP or other OOP languages. Unfortunately, it doesn't provide pointer arithmetic available in C/C++.
Go (aka "Golang") not (Golang aka "Go").