Basics of Golang [For Beginners]
What is Golang?
Golang is a programming language initially developed at Google in year 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. Go programming language is a statically-typed language with syntax similar to that of C. It provides garbage collection, type safety, dynamic-typing capability, many advanced built-in types such as variable length arrays and key-value maps. It also provides a rich standard library.
Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multi core and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It’s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.
Go Programs
A Go program can vary from 3 lines to millions of lines and it should also be written into one or more text files with extension “.go”; for example, hello.go. You can use “vi”, “vim” or any other text editor to write your Go program into a file.
Setting up Go Environment
Environment setup can be done in few steps
Download and install the latest 64-bit Go.Go installable archive file from (which also sets most of the environmental variables for you).
Installation on UNIX/Linux/Mac OS X, and FreeBSD
Extract the downloaded archive into /usr/local, creating a Go tree in /usr/local/go. For example:
tar -C /usr/local -xzf go1.4.linux-amd64.tar.gz
Add /usr/local/go/bin to the PATH environment variable.
OSOutputLinuxexport PATH=$PATH:/usr/local/go/binMacexport PATH=$PATH:/usr/local/go/binFreeBSDexport PATH=$PATH:/usr/local/go/bin
Installation on Windows
Use the MSI file and follow the prompts to install the Go tools. By default, the installer uses the Go distribution in c:\Go. The installer should set the c:\Go\bin directory in windows PATH environment variable. Restart any open command prompts for the change to take effect.
Verify Installation
Now run the test.go to see the result:
Getting Started with Golang
Packages
Every Go program is made up of packages.
Programs start running in package main.
This program is using the packages with import paths “fmt”
Here is an example
Imports
We can write imports as
Or
Functions
The general form of a function definition in Go programming language is as follows:
A function can take zero or more arguments.
In example below , add takes two parameters of type int.
Notice that the type comes after the variable name.
Now run the function.go to see result
Variables
A variable definition means to tell the compiler where and how much to create the storage for the variable. A variable definition specifies a data type and also … continue reading
Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!