Skip to content

Instantly share code, notes, and snippets.

@zelenko
Last active January 17, 2018 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zelenko/c6d26f98591be1aa09c1b7d55b01fec0 to your computer and use it in GitHub Desktop.
Save zelenko/c6d26f98591be1aa09c1b7d55b01fec0 to your computer and use it in GitHub Desktop.
Start coding in Go
# https://gistlog.co/zelenko/c6d26f98591be1aa09c1b7d55b01fec0
published: true
preview: It is easier to write a clean, fast, parallel program in go than in most other languages, and the crucial thing that Go does well is to allow a team to write a clean, fast, parallel program with relative ease.
# published_on: 2017-12-12
include_files: true
/****************************************************************************************
Slice variable does not hold actual value, but it is a pointer to memory address.
Here is an example of how slice can get "corrupted".
This was mentioned in: https://www.upguard.com/blog/our-experience-with-golang
*****************************************************************************************/
package main
import "fmt"
func main() {
array := []string{"a", "b", "c", "d", "e", "f"}
slice1 := array[:3]
slice2 := array[3:]
fmt.Printf("so far so good: slice1 %v slice2 %v\n", slice1, slice2)
slice1 = append(slice1, "BANG")
fmt.Printf("append to slice1: %v\n", slice1)
fmt.Printf("slice2 is now corrupt: %v\n", slice2)
fmt.Printf("full array: %v\n", array)
fmt.Printf("5th element of array: %v\n", array[4])
for j, i := range array {
fmt.Printf("%v ==> %v, \n", j, i)
}
}

Go Report Card

Explore Golang features

This repository is viewable on sourcegraph.com.

Go

Go Resorces

Download GO Packages

Packages are downloaded into folder specified in the $GOPATH system varible:

go get -u gopkg.in/mgo.v2

The 'go get' command requires that the git is installed on Windows.


Systemd

cd /etc/systemd/system/

nano golang.service

[Unit]
Description=Go Server

[Service]
ExecStart=/var/www/web
WorkingDirectory=/var/www/
User=root
Group=root
Restart=always

[Install]
WantedBy=multi-user.target

Start the service

  • systemctl enable golang.service
  • systemctl start golang.service
  • systemctl status golang.service
  • systemctl stop golang.service

Build on Linux

  • export GOPATH=/var/go/web/
  • echo $GOPATH
GOOS=linux GOARCH=amd64 go build -o web

Build on Windows

  • set GOARCH=amd64
  • set GOARCH=386
  • set GOOS=linux
  • set GOOS=windows
  • echo %GOROOT%
go build -o hello.exe hello.go

List of GOOS/GOARCH supported by Go

Installing GO on Debian

cd /usr/local
curl -LO https://redirector.gvt1.com/edgedl/go/go1.9.2.linux-amd64.tar.gz
shasum -a 256 go1.9.2.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.9.2.linux-amd64.tar.gz

vi ~/.profile

export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=/var/go

source ~/.profile

echo $GOROOT

Installing GO on Windows

After downloading and installing, specify where the code is stored in system variable. CLI command: set GOPATH=F:\GoCode

Check existing variables go env. Check current version go version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment