Golang(3)Control Flow and Functions

Golang(3)Control Flow and Functions

2.3 Control Flow and Functions
Control Flow
if x := computedValue(); x > 10 {
…snip...
}else{
…snip...
}

For example:
if x := 100 / 3; x > 10 {
fmt.Println("x is greater than 10,x = ",x)
} else {
fmt.Println("x is less than 10,x)
}

The console output is as follow:
x is greater than 10,x = 33

if integer == 3 {

}else if integer < 3{

}else {

}

goto
I hope I will not use this key word.

for
for expression1; expression2; expression3 {}

sum:=0;
for index:=0;index<10;index++ {
sum += index
}

And
sum: =1
for ; sum < 1000; {
sum +=sum
}

Or alternatively
sum := 1
for sum < 100 {
sum +=sum
}

break and continue

for k,v:= range map {
//map’s key k
//map’s value v
}

If we define k and v and we did not use them both,it will complain Error. So we need to use _ instead.

for _,v :=range map {
fmt.Println(“map’s value: “,v)
}

switch
switch sExpr {
case expr1:
some instructions
case expr2:
some other instructions
case expr3:
some other instructions
default:
other code
}

For example:
i := 10
switch i {
case 1:

case 2,3,4:

default:
...
}

There is new stuff fallthrough,it will execute the current case codes block and continue other case statements.

Functions
func funcName(input1 type1,input2 type2) (output1 type1,output2 type2) {
return value1,value2
}

func max(a,b int) int {
if a > b {
return a
}
return b
}

Multiple Return Values
func SumAndProduct(a,b int) (int,int) {
return a+b,a*b
}

function main(){
x := 3
y := 4
xPlusy,xTimesy : = SumAndProduct(x,y)
}

Or we can define the multiple return values function as follow,give the return value one name
func SumAndProduct(a,b int) (add int,multiplied int) {
add = a + b
multiplied = a*b
return
}

Dynamic Parameters
func myfunc(arg …int) {}

Pass the Value or the Reference (Pointer)
Pass the Value,that is the copy of the parameter,so it will not change the origin object
func add1(a int) int{
a = a+1
return a
}

x :=3
x1:=add1(x)
x1 will be 4
x will still be 3

Pass the Pointer,it will change the original object
func add1(a *int) int {
*a = *a +1
return *a
}

x:=3
x1:=add1(&x) //&x is the address of x
x1 will be 4
x will be 4 too.

defer
Something as follow
func ReadWrite() bool {
file.Open(‘file’)
defer file.Close()
if failureX {
return false
}
if failureY {
return false
}
return true
}

defer run as FILO,not FIFO

Functions are Variables
type can define the function,function is variable. For example:
type typeName func(input1 inputType1,input2 inputType2 [,…]) (result1 resultTyp1 [,...])

We can define a function type,and use this function as a parameters.

package main

import "fmt"

type testInt func(int) bool // 声明了一个函数类型

func isOdd(integer int) bool {
if integer%2 == 0 {
return false
}
return true
}

func isEven(integer int) bool {
if integer%2 == 0 {
return true
}
return false
}

func filter(slice []int,f testInt) []int {
var result []int
for _,value := range slice {
if f(value) {
result = append(result,value)
}
}
return result
}

func main() {
slice := []int{1,2,4,5,7}
fmt.Println("slice = ",slice)
odd := filter(slice,isOdd)
fmt.Println("Odd elements of slice are: ",odd)
even := filter(slice,isEven)
fmt.Println("Even elements of slice are: ",even)
}

The console output from >go run src/com/sillycat/easygoapp/main.go will be as follow:
slice = [1 2 3 4 5 7] Odd elements of slice are: [1 3 5 7] Even elements of slice are: [2 4]

Panic and Recover
…snip…

main and init functions
init can be used in any package,main can only be used in package main.
init will be called every time when we use this function. main will be called in package main automatically.

import
import(
. “fmt"
)

fmt.Println(“”) ——> Println(“”)

import(
f “fmt"
)
fmt.Println(“”) ———> f.Println(“")

import(
_ “github.com/ziutek/MysqL/godrv"
)
Call the init method in that package function.


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

相关文章

程序目录结构 简单实现,用户登录后返回一个jwt的token,下次请求带上token请求用户信息接口并返回信息...
本篇博客的主要内容是用go写一个简单的Proof-of-Work共识机制,不涉及到网络通信环节,只是一个本地的简...
简介 默克尔树(MerkleTree)是一种典型的二叉树结构,其主要特点为: 最下面的叶节点包含存储数据或其...
接下来学习并发编程, 并发编程是go语言最有特色的地方, go对并发编程是原生支持. goroutine是go中最近本...
先普及一下, 什么是广度优先搜索 广度优先搜索类似于树的层次遍历。从图中的某一顶点出发,遍历每一个顶...
第一天: 接口的定义和实现 第二天: 一. go语言是面向接口编程. 在学习继承的时候说过, go语言只有封装,...