Friday 20 December 2013

Dissecting Hello World in Go

In the first post, we wrote a simple hello world application in go. Let's examine it a bit and figure out what all the moving parts are. For reference, here's the code again:
package main
 
import "fmt"
 
func main() {
    fmt.Println("Hello World from Golang")
}
We begin our code with the
package main
command. Packages are exactly what they sound like - discreet pieces of code with a name. You can think of the package command as wrapping up your code into a little bundle so it is ready to be delivered and used by another piece of code. Specifying
package main
tells the compiler that this is the entry point of your code. Our import statement allows us to access other packages - it pulls in or imports other bits of code. Here we import the "fmt" package. This package allows us to create formatted input and output. The setup out of the way, we get to the meat and potatoes our
main
function - as the name suggest, main is the function that will get executed. It is the master control point for us to instruct the computer using golang. Our functionality here relies on the fmt.Println function - this prints a line of text to the standard output of the computer (your screen most likely) and appends a new line. And that's it. One super simple go program

No comments:

Post a Comment