Golang(5)Interface and Concurrence

Golang(5)Interface and Concurrence

2.6 Interface
Interface is a collection of methods.

Interface Type
Define the Interface

type Men interface {
     SayHi()
     Sing(lyrics string)
     Guzzle(beerStein string)
}

type YoungChap interface {
     SayHi()
     Sing(song string)
     BorrowMoney(amount float32)
}

type ElderlyGent interface {
     SayHi()
     Sing(song string)
     SpendSalary(amount float32)
}

interface Value
Men interface variable can store the parameters who implement this interface, for example, Human, Student, Employee instances.

func main(){
     mike := Student{Human{“Mike”, 25, “222-222-xxx”}, “MIT”, 0.00}
     sam := Employee{Human{“Sam”, 36, “444-222-xxx”}, “Golang Inc.”, 1000}
     
     var i Men //define a variable from Interface
     i = mike // i can be a student
     i.SayHi()

     i = sam // i can be employee

     //slice of Men, but different objects
     x := make([]Men, 2)
     x[0], x[1] = mike, sam
     
     for _, value := range x{
          value.SayHi()
     }
}

Empty interface
var a interface{}
a can stores all the different types of instances.

var i int = 5
s:=“hello sillycat”
a = i
a = s

interface Function Parameters
for example, fmt.Println can accept all the parameters who implement string interface

type Stringer interface{
     String() string
}

package main
import (
     “fmt”
     “strconv"
)

type Human struct {
     name string
     age int
     phone string
}

func (h Human) String() string {
     return “<“ + h.name + “ - “ + strconv.Itoa(h.age) + “ years - phone: “ + h.phone + “>"
}

func main() {
     Bob := Human{“Bob”, 39, “000-7777-xxx”}
     fmt.Println(“This Human is : “, Bob)
}

Type of Interface
We know that interface variable can hold all the instances who implement this interface, how will we know that which class is that instance?
Comma-ok
value, ok = element.(T)   value is the value of the variable, ok is bool, lament is the interface variable, T is the type.

For example
list := make(List, 3)
list[0] = 1
list[1] = “Hello”

for index, element := range list {
     if value, ok := element.(int); ok {
          fmt.Printf(“list[%d] is an int and its value is %d\n”, index, value)
     }
     else if value, ok := element.(string); ok {
          fmt.Printf(“list[%d] is a string and its value is %s\n”, index, value)
     }
}

switch Test
The same example as follow>
for index, element := range list {
     switch value := element.(type) {
          case int:
               fmt.Printf(“list[%d] is an int and its value is %d\n”, index, value)
          case string:
               fmt.Printf(“list[%d] is a string and its value is %s\n”, index, value)
          default:
               fmt.Println(“list[%] is of a different type”, index)
     }
}

element.(type) this can only be used in switch statement.

Inner Interface
type ReadWriter interface {
     Reader
     Writer
}

Reflect
Laws of Reflection
http://blog.golang.org/laws-of-reflection

2.7 Concurrence
goroutine
go key word can start a go routine

package main

import (
     "fmt"
     "runtime"
)

func say(s string) {
     for i := 0; i < 5; i++ {
          runtime.Gosched()
          fmt.Println(s)
     }
}

func main() {
     go say("world") //开一个新的Goroutines执行
     say("hello")    //当前Goroutines执行
}

Channels
ci : = make(chan int)
cs := make(cha string)
cf := make(cha interface{})

ch <- v     //send v to channel ch
v := <- ch //receive data from ch, give the value to v

package main

import "fmt"

func sum(a []int, c chan int) {
     total := 0
     for _, v := range a {
          total += v
     }
     c <- total // send total to c
}

func main() {
     a := []int{7, 2, 8, -9, 4, 0}

     c := make(chan int)
     go sum(a[:len(a)/2], c)
     go sum(a[len(a)/2:], c)
     x, y := <-c, <-c // receive from c

     fmt.Println(x, y, x+y)
}

sparkworker1:easygo carl$ go run src/com/sillycat/easygoapp/main.go 17 -5 12

Buffered Channel
ch := make(chan type, value)

value == 0 // no buffer, block
value > 0 // buffer, no block

package main

import "fmt"

func main() {
     c := make(chan int, 5) //修改2为1就报错,修改2为3可以正常运行
     c <- 1
     c <- 2
     fmt.Println(<-c)
     fmt.Println(<-c)
}

Range and Close
for i := range c
close(c)

Select
…snip…

Expiration time
…snip…

Runtime Goruntine
Goexit, Gosched, NumCPU, NumGoroutine, GOMAXPROCS



References:
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/02.6.md

golang 1~4
http://sillycat.iteye.com/admin/blogs/2037798
http://sillycat.iteye.com/admin/blogs/2047158
http://sillycat.iteye.com/admin/blogs/2052936
http://sillycat.iteye.com/admin/blogs/2052937

revel
http://revel.github.io/tutorial/index.html
http://www.cnblogs.com/ztiandan/archive/2013/01/17/2864498.html

martini
http://0value.com/build-a-restful-API-with-Martini
https://github.com/go-martini/martini
https://github.com/PuerkitoBio/martini-api-example

goweb
http://www.giantflyingsaucer.com/blog/?p=4673
https://github.com/stretchr/goweb

http://joshua.themarshians.com/hardcore-google-communicating-go.html

json-go
https://gobyexample.com/json
http://blog.golang.org/json-and-go

你可能感兴趣的:(Scripts)