Skip to content

kirsirinnesalo/go-hello

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Hello World in Go

This is a simple hello world app using Golang.

Golang

Go code is typically kept in a single workspace. Workspace is a directory hierarchy with three directories at its root:

  • src for source files
  • pkg for packages
  • bin for executables

The go tool builds source packages and installs the resulting binaries to the pkg and bin directories.

GOPATH

The GOPATH environment variable specifies the workspace location.

$ mkdir $HOME/work
$ export GOPATH=$HOME/work

For convenience, add bin to the PATH

$ export PATH=$PATH:$GOPATH/bin

Import paths

The packages from the standard library are given short import paths such as "fmt" and "net/http". For your own packages, you must choose a base path that is unlikely to collide with future additions to the standard library or other external libraries.

If you keep your code in a source repository somewhere, then you should use the root of that source repository as your base path. For instance, if you have a GitHub account at github.com/user, that should be your base path.

Hello, World!

To compile and run a simple program, first choose a package path (for example github.com/user/hello) and create a corresponding package directory inside your workspace:

$ mkdir $GOPATH/src/github.com/user/hello

Next, create file named hello.go:

package main

import "fmt"

func main() {
	fmt.Printf("Hello, World.\n")
}

Now you can build and install that program with the go tool:

$ go install github.com/user/hello

hello command is now created and binary is installed to the workspace's bin directory as hello.

Now you can run the program by typing its full path:

$ $GOPATH/bin/hello
Hello, World!

Or, as you have added $GOPATH/bin to your PATH, just type the binary name:

$ hello
Hello, World!

About

Hello world in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages