This article covers the
Step 1 : Installation
Step 2 : Hello World program
Step 1 : Installation
Best way is to look in go site
- Create two directories in your home namely go and bin. (you can set any path). For simple understanding i have created the directory in home folder. If you choose different path than in place of $HOME write your path like /home/abc/golang/go ...
- Open .bashrc file. Normally its at your home. Open $HOME/.bashrc in editor.
- Add following lines (assuming u have 32 bit computer).
export GOROOT=$HOME/go
export GOARCH=386
export GOOS=linux
export GOBIN=$HOME/bin - Open Terminalprogrammer@programmer-laptop:~$ sudo easy_install mercurialprogrammer@programmer-laptop:~$[sudo] password for programmer:Searching for mercurialBest match: mercurial 1.3.1Processing mercurial-1.3.1-py2.6-linux-i686.eggmercurial 1.3.1 is already the active version in easy-install.pthInstalling hg script to /usr/local/binUsing /usr/local/lib/python2.6/dist-packages/mercurial-1.3.1-py2.6-linux-i686.eggProcessing dependencies for mercurialFinished processing dependencies for mercurialprogrammer@programmer-laptop:~$ hg clone -r release https://go.googlecode.com/hg/ $GOROOTprogrammer@programmer-laptop:~$ sudo apt-get install bison gcc libc6-dev ed makeprogrammer@programmer-laptop:~$ cd $GOROOT/srcprogrammer@programmer-laptop:~$ ./all.bash
--- cd ../test N known bugs; 0 unexpected bugs
Lets see what is there in bin directory. There are many executable files. We should see 8g (Compiling), 8l (linking).
The file have an extension .go. So lets create hello.go with following lines
package main //Says its main package
import "fmt" // import
func main() //Entry Point
{
fmt.Print("Hello, World\n"); //Printf /cout/
}
Open Terminal
programmer@programmer-laptop:~$ cat hello.go
package main
import "fmt"
func main()
{
fmt.Print("Hello, World\n");
}
programmer@programmer-laptop:~$ 8g hello.go
programmer@programmer-laptop:~$ 8l hello.8
programmer@programmer-laptop:~$ 8.out
Hello, World
programmer@programmer-laptop:~$
package main
import "fmt"
func main()
{
fmt.Print("Hello, World\n");
}
programmer@programmer-laptop:~$ 8g hello.go
programmer@programmer-laptop:~$ 8l hello.8
programmer@programmer-laptop:~$ 8.out
Hello, World
programmer@programmer-laptop:~$
Simple isn't it !!
8g is go compiler and will create a file with extension .8. To link the file 8l is used and the output goes in to 8.out.
If i want to import more packages with fmt we can do like
To make a variable s of type string
OR
OR
8g is go compiler and will create a file with extension .8. To link the file 8l is used and the output goes in to 8.out.
If i want to import more packages with fmt we can do like
package main
import (
"os";
"fmt";
)
To make a variable s of type string
var s string = "" // With Datatype
OR
var s = "" // No Type needed Compiler will decide
OR
s := "" // Initializing and Declaring
// Single Line comment
/*
MULTI LINE COMMENT
*/
Lets try out another program. This will read command line arguments and displays the same. Like echo command
Open text editor and add following lines
package main
import (
"os";
"flag"; // command line option parser
)
var omitNewline = flag.Bool("n", false, "don't print final newline")
const (
Space = " ";
Newline = "\n";
)
/*
We could have done like
const Space = " "
const NewLine = "\n"
(No need of semi colon here)
*/
func main()
{
flag.Parse(); // Scans the arg list and sets up flags
var s string = "";
for i := 0; i <> 0 {
s += Space
}
s += flag.Arg(i);
}
if !*omitNewline {
s += Newline
}
os.Stdout.WriteString(s);
}
Carefully see the if condition and for loop, its bit syntactically different from what we have seen in c, c++, Java. No need to add condition or for loop parameter in (). But the body should be enclosed in {}.
See the usage of i:=0. No forward declaration is required as := will initialize and assign the value to i and compiler will know what to do with i.
Seems Simple.. But still this is the very very basic things rather not basic also there are lot many features to be seen.
Keep looking will publish the next post with more information and what more in go.
No comments:
Post a Comment