Skip to content

Instantly share code, notes, and snippets.

@budougumi0617
Last active August 23, 2016 17:27
Show Gist options
  • Save budougumi0617/488fe25a2bed08c29fc81b8132099321 to your computer and use it in GitHub Desktop.
Save budougumi0617/488fe25a2bed08c29fc81b8132099321 to your computer and use it in GitHub Desktop.
Golang presentation
$ GOOS=linux   GOARCH=amd64 go build hello.go # For Linux
$ GOOS=darwin  GOARCH=amd64 go build hello.go # For Mac
$ GOOS=windows GOARCH=amd64 go build hello.go # For windows
$ GOOS=android GOARCH=amd64 go build hello.go # For Android
$  go test -cover ./...
ok  	ch01/ex01	0.024s	coverage: 100.0% of statements
ok  	ch01/ex02	0.022s	coverage: 100.0% of statements
ok  	ch01/ex03	0.024s	coverage: 100.0% of statements
ok  	ch01/ex04	0.024s	coverage: 82.1% of statements
ok  	ch01/ex05	0.234s	coverage: 100.0% of statements
ok  	ch01/ex06	0.601s	coverage: 100.0% of statements
$  go test -bench .
BenchmarkMyIntSetAdd100-4              	  200000	      6307 ns/op
BenchmarkMapIntSetAdd1000-4            	   10000	    235164 ns/op
BenchmarkMyIntSetAdd1000-4             	   20000	     64169 ns/op
BenchmarkMapIntSetAddAll10-4           	 1000000	      1253 ns/op
BenchmarkMyIntSetAddAll10-4            	10000000	       134 ns/op
BenchmarkMapIntSetAddAll100-4          	  100000	     14568 ns/op
BenchmarkMapIntSetAddAll1000-4         	   10000	    175950 ns/op
BenchmarkMyIntSetAddAll1000-4          	  200000	      8360 ns/op
$  go test ./...
ok  	github.com/budougumi0617/GoTraining/ch11/ex01	0.019s
ok  	github.com/budougumi0617/GoTraining/ch11/ex02	0.023s
ok  	github.com/budougumi0617/GoTraining/ch11/ex03	0.023s
ok  	github.com/budougumi0617/GoTraining/ch11/ex04	0.027s
ok  	github.com/budougumi0617/GoTraining/ch11/ex05	0.017s
ok  	github.com/budougumi0617/GoTraining/ch11/ex06	0.016s
ok  	github.com/budougumi0617/GoTraining/ch11/ex07	0.014s
package main
import (
"fmt"
"math/big"
"net/http"
)
func main() {
fmt.Printf("Hello, World\n")
}
func getlocal() {
i := returnLocal()
fmt.Printf("Not error %d\n", i)
}
func returnLocal() int {
var local int
local = 10
return local
}
func noboxing() {
var x int
var y int16
x = 10
// y = x // cannot use x (type int) as type int16 in assignment
y = int16(x)
fmt.Printf("y = %d\n", y)
}
func pointer() {
x := big.Int{} // x big.Int type
y := big.NewInt(0) // y *big.Int type(Pointer)
x.SetUint64(123) // x := 123
y.SetUint64(321) // Dont use arrow(->)
}
func math() {
var x complex64
x = 10i + 20
fmt.Println(x) // (20+10i)
}
func multireturn() {
q, r := division()
fmt.Printf("%d, %d\n", q, r) // 3, 1
}
func division() (int, int) {
i := 10
quotient := i / 3
remainder := i % 3
return quotient, remainder // 10 / 3 = 3 ... 1
}
func closure() {
f := func(i int) {
fmt.Printf("Value is %d\n", i)
}
f(20) // Value is 20
}
func cuncurency() {
for i := 0; i < 10; i++ {
go func(num int) {
fmt.Printf("Thread Nomber %d\n", num)
}(i) // Immediate execute
}
}
func channel() {
// connection channel between each gorutine
c := make(chan int)
go func(c chan int) { // Create goroutine
var ans int
// Too large process.
c <- ans // send answer
}(c)
// Recieve answer
fmt.Printf("Result %d\n", <-c)
}
func class() {
md := MyDirectory{name: "Document"}
fmt.Printf("Name is %s\n", md.Name()) // Name is Document
}
// MyDirectory defines Directory.
type MyDirectory struct {
name string
}
// Name returns name.
func (md *MyDirectory) Name() string {
return md.name
}
// MyComponent requests Name() method.
type MyComponent interface {
Name() string
}
func ducktyping() {
var mc MyComponent
mc = &MyDirectory{name: "Users"}
fmt.Printf("Name is %s\n", mc.Name()) // Name is Users
}
func websever() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World!\n")
})
http.ListenAndServe(":8080", nil) // http://localhost:8080/
}
For golang presentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment