Saturday, May 21, 2011

Go ‘Hello World’ - A line by line approach


Create a file called program1.go. The file is completely empty for now. Attempt to compile it as given below.

cmd-prompt>go run program1.go

And you should see this error:
package :
program1.go:1:1: expected 'package', found 'EOF'

In Go, all files have to be associated with a package. For now just understand that there should be a package statement at the top of every .go file and it should be followed by a name of your choice. A package is a good way to logically group various functionality in larger programs. For example, if you were writing a program to build virtual models of vehicles, you could probably put all your car models in a package called cars and all your bus models in a package called buses. Helping to to group related functionality is one of its uses. More on packages later.

Let’s just add a statement now and try:

Full file: program1.go
package main

And you will see this error:
runtime.main: undefined: main.main

Programs in Go needs to be started somewhere, and there should be a recognized way of starting it in some place. Like cars are started with an ignition key and computers are powered on with a power button, programs in Go are started at the main function.

Let’s add another line now as below and try again:

Full file: program1.go
package main

func main() {}

Now let’s compile again and link it as shown below.

cmd-prompt>8g program1.go
cmd-prompt>8l program1.8
cmd-prompt>./8.out
cmd-prompt>


Note that if your system had 6g and 6l as the compiler and linker respectively, you would do this:

cmd-prompt>6g program1.go
cmd-prompt>6l program1.6
cmd-prompt>./6.out
cmd-prompt>


After the compilation using either 8g or 6g, a compiled output is created which has an extension .8 or .6. We then use the linker on this which is either 8l or 6l. When that finishes without error, you get an executable file named 8.out or 6.out.

go run program1.go
As of now it runs successfully, but since we haven’t asked it to do anything particular, it just exits quietly.

At this point, you have your very first working program. Congratulations! It doesn’t do much surely, but it is a valid go program that works.

Let’s go a bit further now and add another line now as below and try again:

Full file: program1.go
package main

func main() {
 Println("Hello world")
}

Attempt to run this now, and you should hit this error.
program1.go:4: undefined: Println

Println is a function that I know prints a string onto the screen. I attempted it, but the compiler says it is undefined. Why so? Remember the packages I mentioned earlier, here is where they start to become useful for us. Functions like Println are present in certain packages. These packages however are not automatically available for our programs though. If we need to reach into the packages to use a certain functionality, we need to import it. Well yes, kind of like we import those foreign cars. You want to use one, well import it first.

Println and certain other functions to read and write text and characters are present in a package called fmt - which is the formatting package.

The Go programming language attempts to be very simple and short. If you are used to programming languages like Java, you are probably used to naming conventions that are lengthier. As a hypothetical example the formatting package might be called “formatting" in a Java convention. Go, on the other hand, attempts to be simple even in the convention. Initially I was not very convinced about it - but having used it for a while, I would say it works very well. Reading code is faster as it is cleaner and I don’t feel condescended to. But that’s just me personally.

So let’s add another couple of lines.

Full file: program1.go
package main

import "fmt"

func main() {
 fmt.Println("Hello world")
}

Now let’s run it with go run program1.go. The output should be:
Hello world

Hello, hello! There you have it. Your first output from a Go program. What we added was just around the packages. We imported the package and then said that the Println can be found in that package by using a dot notation of composition. Let’s talk about that for a moment.

In English, if we wanted to refer to a keyboard that is part of a computer, we would say: computer’s keyboard
In French it would be: clavier de l'ordinateur
In Tamil: computeroode keyboard
In Hindi: computer ka keyboard
In Malayalm: computerinde keyboard
In Go: computer.keyboard

Did you notice that last line? In Go, we use a dot notation with the composing element on the left and the composed element on the right. So more hypothetical examples: Car.Wheel, Mary.Hand, Computer.Screen, Computer.Keyboard.AKey, Computer.Keyboard.AKey.RowNumber. As you can see, the composition can be nested to multiple levels.

13 comments:

  1. Thank you, read an article in the same breath.

    ReplyDelete
  2. In French it's: clavier de l'ordinateur. :D

    ReplyDelete
  3. c'est exact. Merci Sebastien. il est facile à apprendre Go, mais le français ... :-) mais j'essaie, et c'est une belle langue.

    ReplyDelete
  4. In Italian: tastiera del computer ;)

    ReplyDelete
  5. In Kannada: computer Na keyboard :)

    ReplyDelete
  6. "Toetsenbord" in Dutch :-)

    ReplyDelete
  7. In Polish: klawiatura komputerowa

    ReplyDelete
  8. In Russian: Клавиатура компьютера

    ReplyDelete
  9. I am haing some problems. Can you please help me?

    ReplyDelete
  10. This approach seems to be very awesome.

    ReplyDelete
  11. Please change this dialogues from my script. Tao of Badass Book

    ReplyDelete
  12. You have to select which package you want. Teds woodworking Program

    ReplyDelete
  13. learn the codes that are used in CMD PROMPT. DIY Smart Saw Reviews

    ReplyDelete

If you think others also will find these tutorials useful, kindly "+1" it above and mention the link in your own blogs, responses, and entries on the net so that others also may reach here. Thank you.

Note: Only a member of this blog may post a comment.