5. Go的前景
2009年度语言
“ 2015 will be Go's biggest year yet.”
Go 是互联网时代的C语言,不仅会制霸云计算,10 年内将会制霸整个 IT 领域。
OS ARCH OS version
linux 386 / amd64 / arm >= Linux 2.6
darwin 386 / amd64 OS X (Snow Leopard + Lion)
freebsd 386 / amd64 >= FreeBSD 7
windows 386 / amd64 >= Windows 2000
Android NDK
IOS 1.5实验性支持
Windows phone
7. 1.变量
var a int =0 a := 0
var t int64 = time.Now().Unix()
var a , b int =0, 1
var str string = “hello” str := “hello”
var a, str = 0,”hello”
var {
a int = 0
str string=“hello”
}
9. 3.容器(slice , map)
slice(切片)
x := make([]T, LEN, CAP)
sqlTrain := make([]map[string]string, 3)
map
x := make(map[T]T)
m := make(map[string]string)
10. 4.控制结构(if…else,for,range,switch)
if b {
}else{
}
if b {
}else if{
}
for i:=0 ; i < 10; i++ {}
for i , m : = range sqlTrain{}
for k , v := range m{}
11. switch channel {
case 0:
case 1:
case 2:
case 3:
default:
}
switch channel {
case 0,1,2:
case 3:
default:
}
fallthrough
15. type BillsDataRecordT struct {
Id string `json:"id"`
Park struct {
Name string `json:"name"`
Address string `json:"address"`
Code string `json:"code"`
} `json:"park"`
In struct {
Time string `json:"time"`
Channel string `json:"channel"`
Picture string `json:"picture"`
} `json:"in"`
Out struct {
Time string `json:"time"`
} `json:"out"`
Number string `json:"number"`
Charge struct {
Due int64 `json:"due"`
Unpaid int64 `json:"unpaid"`
Duration int64 `json:"duration"`
Paid int64 `json:"paid"`
} `json:"charge"`
}
16. 结构体接口
type M struct {
m []map[float64]map[string]string
}
func (m M) Len() int {
return len(m.m)
}
func (m M) Swap(i, j int) {
m.m[i], m.m[j] = m.m[j], m.m[i]
}
func (m M) Less(i, j int) bool {
var d1, d2 float64
for d1, _ = range m.m[i] {
}
for d2, _ = range m.m[j] {
}
return d1 < d2
}
17. interface{}(接口)(void*)
An interface is a type that can be formally defined by
specifying a particular set of methods.
Interfaces are abstract and cannot be instantiated.
A concrete (i.e., noninterface) type that has the
methods specified by an interface fulfills the
interface, that is, values of such a concrete type can
be used as values of the interface’s type as well as of
their own actual type.
18. type ParkT struct {
Id string `json:"id"`
Park struct {
Name string `json: "name"`
Code string `json: "code"`
Address string `json: "address"`
Gps string `json: "gps"`
Type string `json: "type"`
Open_time string `json: "open_time"`
Criterion string `json: "criterion"`
} `json:"park"`
Distance float64 `json: "distance"`
Space struct {
Total int `json: "total"`
Empty int `json: "empty"`
} `json:"space"`
Payment struct {
Online_pay int `json: "online_pay"`
Cash int `json: "cash"`
UnionPay int `json: "unionpay"`
Alipay int `json: “alipay"`
Weixin int `json: "weixin"`
Wallet int `json: “wallet"`
} `json: "payment"`
}
19. len()
str := “京A00000”
fmt.Printf(“%dn”,len(str))
sqlTrain := make([]map[string]string,0)
fmt.Printf(“%dn”,len(sqlTrain))
a :=[…]int{}
a :=[…]int{1,2,3}
a :=[3]int{1,2,3}
fmt.Printf(“%dn”,len(a))
20. The len built-in function returns the length of v, according to its type:
Array: the number of elements in v.
Pointer to array: the number of elements in *v (even if v is nil).
Slice, or map: the number of elements in v; if v is nil, len(v) is zero.
String: the number of bytes in v.
Channel: the number of elements queued (unread) in the channel buffer;
if v is nil, len(v) is zero.
21. append()
The append built-in function appends elements to the end of a slice. If
it has sufficient capacity, the destination is resliced to accommodate the
new elements. If it does not, a new underlying array will be allocated.
Append returns the updated slice. It is therefore necessary to store the
result of append, often in the variable holding the slice itself:
slice = append(slice, elem1, elem2)
slice = append(slice, anotherSlice...)
As a special case, it is legal to append a string to a byte slice, like this:
slice = append([]byte("hello "), "world"...)
22. copy()
The append built-in function appends elements to the end of a slice. If
it has sufficient capacity, the destination is resliced to accommodate the
new elements. If it does not, a new underlying array will be allocated.
Append returns the updated slice. It is therefore necessary to store the
result of append, often in the variable holding the slice itself:
slice = append(slice, elem1, elem2)
slice = append(slice, anotherSlice...)
As a special case, it is legal to append a string to a byte slice, like this: slice =
append([]byte("hello "), "world"...)