Create your first GO project

Hello,

In this article, I will explain how to run a GO project from scratch in a Linux environment. I would like to mention that I used the official GoLang website as a reference for this article. Without further ado, let’s get straight to what we need to do.

Firstly, as stated on the website, we need the following system components:

Operating systemArchitecturesNotes
FreeBSD 10.3 or lateramd64, 386Debian GNU/kFreeBSD not supported
Linux 2.6.23 or later with glibcamd64, 386, arm, arm64,
s390x, ppc64le
CentOS/RHEL 5.x not supported.
Install from source for other libc.
macOS 10.10 or lateramd64use the clang or gcc that comes with Xcode for cgo support
Windows 7, Server 2008R2 or lateramd64, 386use MinGW (386) or MinGW-W64 (amd64) gcc.
No need for cygwin or msys.

If there are no issues, we need to continue by installing the Go tools. Download the appropriate compressed file for your system from the following address. I will proceed with the explanation using Linux.

Open the downloaded file for Linux with the following command and install it in the specified directory. Note that you might need administrative privileges to run this command.

tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz

This way, we have completed the installation of GO in the /usr/local directory. Now we need to set the PATH for GoLang. Run the following command to set the PATH:

export PATH=$PATH:/usr/local/go/bin

Now we can write our first program with GoLang. In a directory of your choice, create the hello.go file below. Edit the file using an editor and add the following code block to the hello.go file.

import "fmt" 

func main() { 
   fmt.Printf("hello, world\n") 
}

With the above code, we have created a Go program that prints “hello, world” to the screen. Now we need to compile our code. Use the following command to compile our program:

go build

After the compilation process is completed, we can run our program and see the “hello, world” message displayed on the console using the following command:

go run hello.go

Note: When using the “go run” command directly without using the “go build” command, our program will be compiled and run directly. Happy coding!