From charlesreid1

 
(4 intermediate revisions by the same user not shown)
Line 50: Line 50:
* PATH - update PATH to include the binary folder of both GOROOT and GOPATH
* PATH - update PATH to include the binary folder of both GOROOT and GOPATH


For example, if Go were installed to the home directory in <code>go/</code> and <code>work/</code>, these lines would be added to the user's <code>.profile</code> file:
For example, if Go were installed to the home directory in <code>~/go/</code>, and the user kept Go projects in <code>~/work/</code>, these lines would be added to the user's <code>.profile</code> file:


<pre>
<pre>
Line 62: Line 62:
Now that Go is installed and the paths are set for your server, you can test to ensure that Go is working as expected.
Now that Go is installed and the paths are set for your server, you can test to ensure that Go is working as expected.


Create a new directory for your Go workspace, which is where Go will build its files:
Create Go workspace, which is where Go will build its files:


     mkdir $HOME/work
     mkdir $HOME/work


Then, create a directory hierarchy in this folder in order for you to create your test program file. We’ll use the directory my_project as an example.
Create a directory hierarchy. Use <code>my_project</code> as an example top-level directory for this example, and a hello subfolder:


     mkdir -p work/src/my_project/hello
     mkdir -p work/src/my_project/hello


Next, you can create a traditional "Hello World" Go file.
Now create a traditional hello world Go file:


     nano ~/work/src/my_project/hello/hello.go
     vim ~/work/src/my_project/hello/hello.go


Inside your editor, paste the code below, which uses the main Go packages, imports the formatted IO content component, and sets a new function to print "Hello, World" when run.
Edit this file and add the contents:
hello.go
 
'''<code>hello.go</code>:'''


<pre>
<pre>
Line 87: Line 88:
</pre>
</pre>


This program will print "Hello, World!" if it successfully runs, which will indicate that Go programs are compiling correctly. Save and close the file, then compile it by invoking the Go command install:
Now run this via:
 
<pre>
go install my_project/hello
</pre>
 
You should see your Go program when you type which hello:
 
<pre>
$ which hello
/home/user/work/bin/hello
</pre>
 
Now test:
 
<pre>
$ hello
Hello, World!
</pre>
 
Also see https://golang.org/doc/code.html (section: Your First Program)
 
=Flags=
 
{{GoFlag}}

Latest revision as of 01:29, 19 December 2018

Installing Go

Linux

Visit the Go downloads page: https://golang.org/dl

Get the URL for the latest version of Go

Download go:

curl -O https://dl.google.com/go/go1.11.2.linux-amd64.tar.gz

Verify checksum:

sha256sum go*.tar.gz

Now you're ready to install Go:

tar xvf go*.tar.gz

sudo chown -R root:root ./go

sudo mv go /usr/local

Mac

brew install go
brew upgrade go

Goenv

Following the successful model of pyenv, you can use goenv to set up and manage various side-by-side versions of Go.

Link: https://github.com/syndbg/goenv

Paths

Edit your .profile to make the following changes to your environment variables:

  • GOROOT - (by default, this is /usr/local, only change if you are using a different location) the location of your Go tree
  • GOPATH - this is a list of paths in which Go code, packages, objects, and executables are stored
  • PATH - update PATH to include the binary folder of both GOROOT and GOPATH

For example, if Go were installed to the home directory in ~/go/, and the user kept Go projects in ~/work/, these lines would be added to the user's .profile file:

export GOROOT=$HOME/go
export GOPATH=$HOME/work
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Testing

Now that Go is installed and the paths are set for your server, you can test to ensure that Go is working as expected.

Create Go workspace, which is where Go will build its files:

   mkdir $HOME/work

Create a directory hierarchy. Use my_project as an example top-level directory for this example, and a hello subfolder:

   mkdir -p work/src/my_project/hello

Now create a traditional hello world Go file:

   vim ~/work/src/my_project/hello/hello.go

Edit this file and add the contents:

hello.go:

package main

import "fmt"

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

Now run this via:

go install my_project/hello

You should see your Go program when you type which hello:

$ which hello
/home/user/work/bin/hello

Now test:

$ hello
Hello, World!

Also see https://golang.org/doc/code.html (section: Your First Program)

Flags