How To Write A Hello World Program In Google Go

July 26th, 2010 by Kevin | Posted under Go Programming.

In the previous post, we saw how to install Google’s Go programming language.

Learning any programming language begins with the “Hello World” program. So keeping with the tradition and assuming that you have some programming background, here is the program written in the Google Go programming language.

Open your favorite text editor and type the following program in it.

package main

import "fmt"   //Package implementing formatted I/O

func main(){
   fmt.Printf("hello, worldn")
}

Save the file as “hello.go”.

From the console, compile it using

$ 6g hello.go

To link the file, use

$ 6l hello.6

and to run it

$ ./6.out

This will print,

hello, world

The first line in the program

package main

specifies the name of the package that the file “hello.go” belongs to. The package keyword is used to define the package.

This program imports the package “fmt” to gain access to fmt.Printf. We shall see more about packages in later tutorials.

import "fmt"

Functions are introduced with the func keyword. The main package’s main function is where the program starts running (after any initialization). We shall learn more about functions in later tutorials.

func main()

String constants can contain Unicode characters, encoded in UTF-8. The ‘n’ is the newline character as in C/C++.

The comment convention is the same as in C++:

/* ... */
// ...

Did you find this tutorial on the Google Go programming language useful? Would you want more of such tutorials for learning about this great new programming language by Google?

 

Related Posts:

Tags:

Do you have any comments on How To Write A Hello World Program In Google Go ?

Spam protection by WP Captcha-Free