Skip to content

Instantly share code, notes, and snippets.

@nitya
Last active October 27, 2015 09:00
Show Gist options
  • Save nitya/e3b73b9f7c26de6ffa16 to your computer and use it in GitHub Desktop.
Save nitya/e3b73b9f7c26de6ffa16 to your computer and use it in GitHub Desktop.
WomenWhoGo-GoTutorials: Troubleshooting

This gist documents steps taken to troubleshoot or fix issues raised when following the [WomenWhoGo Tutorials] (https://github.com/womenwhogonyc/Go-Tutorial)

Install Go:

brew install go

Set Environment Variables:

export GOPATH=$HOME/GoLang
export PATH=$PATH:$GOPATH/bin

Set Directory Structure:

$GOPATH
├── bin
├── pkg
└── src
  └── hello
     └── hello.go

Build in LOCAL directory (for local usage)

cd $GOPATH/src/hello
go build hello.go
./hello

Install (for global usage)

go install hello
hello

Note that for the "global" install, you MUST have the hierarchy set up above. In particular, the install command looks within the $GOPATH/src directory for the content to be installed. i.e., the command is in the form

go install <project>

where "" is the relative-path to the project from the $GOPATH/src root. So, in the above case, if your "hello" project were to be moved to "src/tutorials/hello" then you would use "go install tutorials/hello" to build and install the resulting binary.

The resulting binary is then installed in $GOPATH/bin

Demonstrates

  • "fmt" (formatted io) commands similar to printf/scanf
  • build, install and run of Go commands

Fairly straightforward. Nothing complex to report.

Demonstrates

  • "fmt.Scanln" (formatted input) to take command line input

Also fairly straightforward.

mkdir webserver
cd webserver

// edit webserver.go

go install webserver
webserver

Demonstrates

  • "io.Writeln" and use of Package io for interfaces to I/O primitives
  • use of Package http for HTTP client and server implementations
  • "http.HandleFunc" to handle routes
  • "http.ListenAndServe" to handler server sockets

Go routines

If you want to code out the example they show up front, you will need to first add the required "package" and "import" statements. See the following for example:

package main

import (
	"fmt"
	"time"
)

func main() {
    go printWord("hi")
    time.Sleep(3 * time.Second)
}

func printWord(s string) {
    fmt.Println(s)
}

Demonstrates: goroutines Any function can be made to run in a separate thread by simply calling it with "go".

Channels

Channels provide a communication/messaging pipe for data transfer between endpoints.

Channels can be unbuffered (=> blocks sender till received exists to take value)

ch := make(chan int)

or buffered (=> with specified size, does not block sender as long as buffer slots are open)

ch := make(chan int, 100)

Send value into channel

ch <- val

And receive value

val := <-ch
@Lyoness
Copy link

Lyoness commented Sep 20, 2015

Thank you! <3

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