Skip to content

Instantly share code, notes, and snippets.

@yingmu52
Last active November 12, 2017 21:40
Show Gist options
  • Save yingmu52/5461518857db1828417a43dde228f836 to your computer and use it in GitHub Desktop.
Save yingmu52/5461518857db1828417a43dde228f836 to your computer and use it in GitHub Desktop.
Setup Go Environment

Install & Setup

Note: all the go code should be inside this go folder

  • Set your GOPATH
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
echo "export GOPATH=$HOME/go" >> .bash_profile
  • Check if install & setup successfully
  1. Create hello.go in ~/go/src/hello/ with the following code
package main
import "fmt"
func main() {
  fmt.Println("ok")
}
  1. Then go run hello.go
ok
@yingmu52
Copy link
Author

yingmu52 commented Nov 12, 2017

Working with JSON

Marshal vs UnMarshal

type person struct {
	First string `json: "First"`
	Last  string `json: "Last"`
	Age   int    `json: "Age"`
}

func main() {
	// struct to json
	p1 := person{"james", "bond", 32}
	p2 := person{"money", "pendy", 38}
	people := []person{p1, p2}
	fmt.Println(people)
	bs, err := json.Marshal(people)
	if err != nil {
		fmt.Println(err)
	}
	jsonString := string(bs)
	fmt.Println(jsonString)

	// json to struct
	bs1 := []byte(jsonString)
	var people1 []person
	err1 := json.Unmarshal(bs1, &people1)
	if err1 != nil {
		fmt.Println(err1)
	}
	fmt.Println(people1)
}

@yingmu52
Copy link
Author

Concurrency is Not Parallelism, it is a design pattern that makes your code to run better on Multiple Core Computer

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