DEV Community

Cover image for golang compiler
bluepaperbirds
bluepaperbirds

Posted on

golang compiler

If you have the Go programming language installed on your computer, you can run Go programs. You can verify this by opening a terminal and typing

go version

If it outputs an error like "command not found", you should install it.

Then you can save your program as filename.go (the .go extension) and run it with:

go run filename.go

That then runs the program and outputs it in the terminal. What if you want to have a program?

Then you have to compile it. Compiling converts your source code into an actual program.

Compile in Go

You can do so in a few steps. Open up a terminal (On Linux: Ctrl+Alt+t).
First create new directory for your project:

➜  ~ mkdir hello
➜  ~ cd hello

Now create your hello world program with your favorite editor.

➜  hello vim hello.go

Do note I'm using a Zsh theme so it puts the directory name in front.
The hello world program:

package main

import "fmt"

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

Save it and exit the editor.
Now you can run the build command:

➜  hello go build hello.go

And you have an application that you can run

➜  hello ./hello
Hello, world.

That's it! Simple

Top comments (0)