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