Getting started with Go programming

For centuries we have evolved in expressing ourselves and languages are important factor during our evolution and this continues in the digital age too. Interestingly the evolution of programming language has been much rapid as compared to the spoken language. Few languages that lasts decade such as C and there are many which been dominant in the 90s and gets replaced like for example perl. Yet there is always a new language poping in the market. If the emergence of Java, Python and Lua rocked the 90s then these languages are rocking the 20th century – Scala, Swift, Go and Rust. In them the Scala and Swift came as a replacement for Java and Objective-C but still executing on the same run-time. Rust is a systems programming language sponsored by Mozilla Research. It is designed to be a “safe, concurrent, practical language”,supporting functional and imperative-procedural paradigms. Rust is syntactically similar to C++, but is designed for better memory safety while maintaining performance. The topic of our focus is the Go language.

Before going in to any lets install the go language in Debian Linux and continue further.

$sudo apt-get install golang-go

The below command should give the following output with version showing the version of the go language you installed.

$go version
go version go1.3.3 linux/amd64

Go is a free and open source programming language created at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson aka ken who is famous for creating the languages B the predecessor   C and Unix. When we use the name GO it should not be confused with another language with a same name Go!. The difference here it just an exclamation point !.

package main
import "fmt"
func main() {
fmt.Println("hello world")
}
$go run helloworld.go
Output:
hello world

when you type go run command it compiles the code in a temp folder and executes. Go is a compile and execute type of language.  You can also compile it just like C or C++ by using the below command

$go build helloworld.go
#This will output helloworld binary file
$ file helloworld
helloworld: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

Code explanation:

package main

The first line is a package declaration. Every Go program must start with a package declaration. Go language handles the packages/library/modules this way. The entry point for the application is the main function in the main package.

import “fmt”

The import keyword is used to include the library from other packages.  The fmt package implements formatting for input and output. More information about the fmt package can be found here.

func main() {

a function starts with a keyword func and this is the main function which is part of the main package. It start and ends with flower brackets  like this {  }.

fmt.Println(“hello world”)

Println is a print line function which prints the value supplied in the arguement. There are other functions such as Printf for example

fmt.Printf(“%d\n”, 123)

The Go language provides two types of commenting .

/*

fmt.Printf(“%d\n”, 123)

*/

or

//fmt.Printf(“%d\n”, 123)

You can download the source code from https://github.com/codelectron/GoLangSeries .

3 thoughts on “Getting started with Go programming

Leave a Reply

Your email address will not be published. Required fields are marked *