Hello World! - Writing your first Go program

in #golang7 years ago (edited)

Hello everyone!

Let's guide you to create your first program in Go. The first program in most programming languages would be a program that writes Hello World on your screen.

To get started, first we need to install Go. If you are on Windows or macOS, go to http://golang.org and find the correct download link for your platform. Follow the steps in the installer. If you are on Linux, you can do the same, but usually you would install the version in the package manager.

Go can install dependencies automatically from e.g. Github, but it needs to know the place to put everything. This place is called the GOPATH, you can configure an environment variable to have this set to where you want, but from Go 1.8 this will be automatically set to $HOME/go. You would put your own code in this directory under $GOPATH/src/github.com/youraccount/yourproject. You can also put code in other locations on your disk, but it will complicate some things.

To compile code with Go, we need the command line. In Windows you would do Windowskey+R, then type cmd then type enter. In macOS you start terminal.app. Also we need a text editor. If you don't have a nice one yet, on Windows I would recommend to download and install notepad++, or work with a Go-oriented text-editor like LiteIDE.

Now create a new file with the following contents, save it under any name you want ending at .go (for example helloworld.go).

package main // this is the package name, and // tells us
// that this is a comment.
// to make a command that runs from the commandline,
// we need to put it in the package called main

import "fmt" // we import the package "fmt"
// which has format functions,
//among them Println to put something on the screen

func main() { // the program starts in the function called main
     fmt.Println("Hello world!")  // To run a command from a package,
// we have to prefix it.
// You can find the commands in a package at godoc.org
     fmt.Println("你好,世界!") // Go also supports Unicode
}

Then on the command line go where your file is (cd path changes your directory to path). Then you can type go run helloworld.go to compile and run it directly, or go build eventually followed by the file name to build it first, then ./helloworld to run it (you don't need the ./ on Windows).

Now you should see something appear on the screen. What do you see? Can you change the program and make it show something different?

Sekvu min!

Sort:  

So go is another C style syntax?

Might be worth tweaking the new lines in you code block. I find I have to make compromises and wrap code to stop the horizontal scroll bars. Reads better when you do not need to scroll

Yes, Go has C-like syntax, just because people are most accustomed to it. Technically it also has semicolons, but the compiler puts them in. Don't worry about the formatting, the gofmt tool takes care of that.

Go it's an interesting Language as Rust, D, etc.

If you don't like C style syntax, I've created a post getting started with Eiffel ;)