Go! Go! Go! :Intro to emerging “GoLang”

07 / Jul / 2014 by Sahil Chitkara 0 comments

GO! GO! GO!

Go is an open source programming language initially developed by GOOGLE .When we say about key features,they are a good mixture of multiple languages like statically-typed language with syntax loosely derived from C, type safety, garbage collection, dynamic typing and contains large standard library.

Through go,  Google desires to keep language specification as simple as possible so that it can be easily hold by programmer ,by omitting some features like in other languages.Those features are like

1. No Type inheritance
2. No method and operator overloading
3. No pointer Arithmetic
4. No assertions
5. No generic programming

Basically the go follows the tradition of C, but makes many changes aimed at conciseness, simplicity, and safety.For more details of go i will be writing more and more blogs for some important concepts.

Installing Go!

Download the archive file from golang website for your OS.

Extract the archive in "/usr/local" directory.
[js]
tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
[/js]

Add /usr/local/go/bin to the PATH environment variable: export PATH=$PATH:/usr/local/go/bin

For installing go to custom location
[js]
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin</pre>
[/js]

GO is ready to use 🙂

As Programming world has a tradition of “Hello world” Program,We must have a demo for the same
So,create a file, lets say main.go

open the file and write the following lines of code give below

[js]
package main

import "fmt";

// this is a comment

func main() {
fmt.Println("Hello World")
}
[/js]

Now to run go program : go run main.go
Result: Hello World
The go run command takes the subsequent files one or many(separated by spaces), compiles them into an executable saved in a temporary directory and then runs the program.

What’s with all the jargon?

Package

[js]
package <packageName>
[/js]

Every go program must have package statement in first line.Go program can be of two types “Executable or libraries”.Executable are those which can be executed on terminal, and libraries are those lines of code which are packaged together and will be used in other programs.The go file you are going to execute on terminal must have “package main”

import

[js]
// Single import
import <packageName>

//Multiple imports
import (
"package1"
"package2"
)
[/js]

Import keyword is used to import code from other packages to use with our program. “fmt” is shorthand of format implements formatting of input and output.
we can import multiple packages in single import statement in following manner

[js]
//Example
import (
"fmt"
"io/ioutil"
)
[/js]

func

func denotes function (building blocks of go) they have input ,output and lines of code called statements executed in order

Signature

[js]
func <function name>(<arguments>){
// Go statements here ….
}
[/js]

similar to c the name main is special because it’s the function that gets called when you execute the program.

Println

I think this derived from java (Apart from the casing 😉 ).

[js]
fmt.Println("Hello World")
[/js]

package fmt contains a function println,takes an argument of string type.Println prints the string passed in argument on the terminal on execution of program.

So this was the simple demo and introduction to golang.I hope you like it.

Keep a watch for new, interesting and more detailed features of golang in my upcoming blogs.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

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