“Hello World!” – Basic of Basic Example

(10 minutes for reading and installing)

Install Golang on your PC

1. Download and Install Golang into your PC from link, we have many version for many OS -> you can choose stable version with your OS PC

2. After installing completely, you can check by open terminal(mac os, linux, ubuntu) or cmd line window(on Window OS) to check. Here, I’m using Mac OS with terminal window

3. And you need to check environment of Golang in your PC with cmd line

You need to pay attention to GOPATH, that is the path you need to put code golang into directory $GOPATH/src to start Golang code and run it. In my PC, that is “/Users/autononous/go/”

OK, nice. Now, you will see in $GOPATH, we have 3 directories, and you only pay attention to src, this is place you start to code

4. Go to src directory. In here, create a new directory with name $GOPATH/src/hello_world

5. Continue move into hello_world and create a file with extension look like hello-world.go in here

Nice and now, you can start first codes

Your first lines code with “Hello World” program

Open hello-world.go with any text editor in your PC which you have (notepad, nano, vim and I will introduce some advance editor in the end of this article …)

Our first program will print the classic “hello world” message. Here’s the full source code.

To run the program, write the code(box on your left hand) into hello-world.go by editor and use go run.

Sometimes we’ll want to build our programs into binaries. We can do this using go build.

We can then execute the built binary directly.

package main
import "fmt"
func main() {
    fmt.Println("hello world")
}
$ go run hello-world.go
hello world

$ go build hello-world.go
$ ls
hello-world    hello-world.go

$ ./hello-world
hello world

IDE (or Integrated Development Environment) – a good Advance Editor for Programmer

IDE is a software which you can use to code, run and build you program easily. IDE support a cool editor, remind code, check grammar of code automatically, … That is a really good tool for Programmer

With Golang, I choose Goland of Jetbrain to use. You only need to download and install into PC. Open it

Click “Open Project” and choose hello_world directory

Nice to use

To run your code, only need to Right Click on go file and choose Run ‘go build hello-world.go’ like this

And you have a good result as you run on cmd line window

View on github

Next

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s