hello world golang

Hello World Golang and Go tool

This tutorial demonstrates on how to develop a simple Go package and how to use the go tool (command go) to fetch, build and install Go packages and commands. In order to use go tool you need to organize your Go code (Please check Golang Project Structure ) in a specific way.

hello world golang

Source Repository and Base Path

Let’s say if you want to keep your code in some source repository like GitHub, then you should use the root of that source directory as your base path. For instance, if you have github account github.com/sneppets , then that is your base path.

Now use  github.com/sneppets  as your base path and create a directory inside your workspace where you want to keep the source code:

Hello World Golang

First create/choose a package path (let’s use github.com/sneppets/hello) and create corresponding package path in your workspace.

Next create a file named  hello.go inside that directory and write the following code

Using go tool

You can use go tool (command go) to build and install your hello world program. The below command finds the source code by looking for the package github.com/sneppets/hello inside the workspace specified by GOPATH.

You can also use  go install command from the package directory

As a result this command builds hello command, producing an executable binary hello (in windows hello.exe)in the bin directory

Go commands

Now you can run the program by typing the following command

If you have added  $GOPATH/bin  to your PATH, then you can just type the binary name from the command line like below

Finally you can push the code to remote repository (GitHub in this case)



Recommended for you

How to create Golang project structure

Leave a Reply

Notify of