|
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/ |
|
} |