D_Kim

## 基本语法

### array数组

``````// 第一种
var x [10]int
x[1]=1
x[9]=10
fmt.Printf("%v",x)
// 第二种
x:=[10]int{1,10}
fmt.Printf("%v\n",x)
fmt.Printf("%v",len(x))
``````

### slice动态数组

``````var x []int
``````

``````s:=make([]int,3,5)
fmt.Printf("%v", len(s))
``````
``````3
``````

3为数组长度，5为建议容量 实际使用中如果超过这个值 底层会自动扩展的

``````s :=make([]int,3,5)
s = append(s,1,2,3,4)
fmt.Printf("%v", s)
``````
``````[0 0 0 1 2 3 4]
``````

``````  x :=[10]int{1,2,3,4,5,6,7,8,9,10}
y :=x[1:3]
fmt.Printf("%v",y)
``````
``````[2 3]
``````

### map

map必须要make

1.

``````var student map[string]int
student = make(map[string]int)
student["zhangsan"]=1
fmt.Printf("%v", student)
``````
``````map[zhangsan:1]
``````
``````student := make(map[string]int)
student["zhangsan"]=1
fmt.Printf("%v", student)
``````
``````map[zhangsan:1]
``````

### range

key和value都遍历

``````student := make(map[string]int)
student["zhangsan"]=1
student["lisi"]=2
student["wangwu"]=3
for i,v := range student{
fmt.Println(i,v)
}
``````
``````zhangsan 1
lisi 2
wangwu 3
``````

``````x := [3]int{1,2,3}
for _,v := range x {
fmt.Println(v)
}
``````
``````1
2
3
``````

``````x := "zhangsan"
for _,v := range x{
fmt.Printf("%c\n", v)
}
``````
``````z
h
a
n
g
s
a
n
``````

## 函数

### 简单函数

``````func main()  {
a :=1
b :=2
a,b = swap(a,b)
fmt.Printf("%d %d", a,b)
}

func swap(a int, b int) (int,int) {
return b,a
}
``````
``````2 1
``````

``````func getSum(a []int)  (int){
sum := 0
for _,v := range a {
sum+=v
}
return sum
}

func main()  {
a := []int{1,2,3}
s := getSum(a)
fmt.Println(s)
}
``````
``````6
``````

### 匿名函数

``````func main()  {
f := func (x,y int) int  {
return x+y
}

fmt.Println(f(1,2))
}
``````
``````3
``````

### defer

``````func main()  {
for i :=1 ; i<5; i++ {
defer fmt.Println(i)
}
fmt.Println("before defer")
}
``````
``````before defer
4
3
2
1
``````

defer 后面跟着语句块是函数执行结束之前再执行,一般可以用在资源的关闭上。例如:

``````file.Open("file")
defer file.Close()
``````

defer和匿名函数一起用

``````func main()  {
defer func ()  {
fmt.Println("after defer")
}()
fmt.Println("before defer")
}
``````
``````before defer
after defer
``````

'()'匿名函数后面的（）表示执行这个函数的意思等价于下面

``````func main()  {
f := func ()  {
fmt.Println("after defer")
}
defer f()
fmt.Println("before defer")
}
``````

## 对象

### struct

``````type Person struct{
name string
age int
}
type Student struct{
Person
speciality string
}
func main()  {
student := Student{Person{"zhangsan",18},"math"}
// student := Student{speciality:"math",Person:Person{"zhangsan",18}}
fmt.Printf("%v\n", student)
fmt.Printf("%v\n", student.speciality)
fmt.Printf("%v\n", student.age)
fmt.Printf("%v", student.Person.name)
}
``````
``````{{zhangsan 18} math}
math
18
zhangsan
``````

### 类的初始化

``````//  指针变量
point := new(Point)
point := &Point{}
point := &Point{x:100,y:100}

// 实例
point := Point{}

``````

### 类的方法

``````type Point struct{
px float32
py float32
}

func (point *Point) setXY(px,py float32)  {
point.px = px
point.py = py
}

func (point *Point) getXY() (float32,float32)  {
return point.px,point.py
}

func main()  {
point := new(Point)
point.setXY(1.2,3.4)
px,py := point.getXY()
fmt.Print(px,py)
}
``````
``````1.2 3.4
``````

### 接口

``````type Animal interface{
Fly()
Run()
}

type Bird struct{
}

func (bird Bird) Fly()  {
fmt.Println("Bird is flying!!!")
}

func (bird Bird) Run()  {
fmt.Println("Bird is running!!!")
}

func main()  {
var animal Animal
bird := new(Bird)
animal = bird
animal.Fly()
animal.Run()
}
``````
``````Bird is flying!!!
Bird is running!!!
``````

``````type Animal interface{
Fly()
Run()
}
type Animal2 interface{
Fly()
}

type Bird struct{
}

func (bird Bird) Fly()  {
fmt.Println("Bird is flying!!!")
}

func (bird Bird) Run()  {
fmt.Println("Bird is running!!!")
}

func main()  {
var animal Animal
var animal2 Animal2
bird := new(Bird)
animal = bird
animal2 = animal      //划重点
animal2.Fly()
}
``````

``````func main()  {
var v1 interface{}
v1 = 6.78
if v,ok :=v1.(float64); ok {
fmt.Println(v)
}

var v2 interface{}
v2 = 12
if v,ok := v2.(float32); ok{
fmt.Println(v)
}else {
fmt.Println("no float32")
}
}
``````
``````6.78
no float32
``````
``````func main()  {
var v1 interface{}
v1 = 6.78
switch v1.(type) {
case int:
fmt.Println("this is int")
case float32:
fmt.Println("this is float32")
case float64:
fmt.Println("this is float64")
}
}

``````
``````this is float64
``````

### D_Kim

React Native \ WEEX 开发文档汇总

React-Native 开发基础文档 环境构建 参照官方提供文档的指引 集成到现有应用 基本语法 入门基础 组件及api JsX语法基础 React-Native生命周期 ES6基础 promise async/await 状态管理 Redux状...

2018/01/11
0
0
Python基础入门教程，Python学习路线图

2017/07/11
933
1
GO从入门到进阶教程系列 - 研发高性能ORM框架入门

aielves
03/16
0
0
2019年应该如何学习前端开发？

WEB开发阿靖
02/16
0
0
python基础语法

2015/12/11
48
1

17分钟前
3
0

20分钟前
6
0
Yii 框架中rule规则必须搭配验证函数才能使用

public \$store_id;public \$user_id;public \$page;public \$limit;public \$list;public \$mch_list;public \$cart_id;public \$is_community;public \$shop_id;public \$cart_typ......

chenhongjiang
22分钟前
4
0
Flutter使用Rammus实现阿里云推送

EmilyWu
23分钟前
42
0
Knative 实战：三步走！基于 Knative Serverless 技术实现一个短网址服务

38分钟前
2
0