Monday, September 14, 2015

First Program

With hardly 25 keywords, it is obvious that Go will impose a bit more complicated signature for its language constructs. This becomes evident as soon as you write your first program.

package main 

import (
   "fmt",
   "runtime"
)

func main() {
   fmt.Println("Hello from", runtime.GOOS)
}

The main keyword is reserved to serve as the entry point. Go will search for a package named "main" and execute the function named "main" in it to bring your application to life.

This implies that the file name serves no purpose other than to recognise a package name at directly level. You can have a file name "obama.go" with a package named "osama" and go won't punish you.

To expose any function from the package, capitalise its name. Go is case sensitive.

The main function takes no argument and doesn't return any value (except when the application quits, and it returns 0 to OS to indicate it exited)

The compiler does have a "Println" method implementation but since compilers are written for a particular OS they dont provide a universal way of print line implementation. "fmt" package does.

go run hello-world.go command compiles the code, stores the executable in some temporary location and then runs that executable.





No comments:

Post a Comment