Go语言练习

Go语言的斐波那契数量。

package main
import (
    "fmt"
)
func fibonacci(n int) int {
    if n <= 2 {
        return 1
    }
    return fibonacci(n-1) + fibonacci(n-2)
}

func main() {
    for i := 1; i <= 10; i++ {
        fmt.Println(fibonacci(i))
    }
}
package main
import (
    "fmt"
)

func fib(n int) int {
    one, two := 1, 0
    for i := 1; i < n; i++ {
        one, two = one+two, one
    }
    return one
}

func main() {
    for i:= 1; i <= 10; i++ {
        fmt.Println(fib(i))
    }
}
package main
import(
    "fmt"
)
var se chan int
func f() int {
    return <-se
}

func lazy() chan int {
    temp := make(chan int)
    one, two := 1,0
    go func() {
        temp <- one
        for {
            one, two = one+two, one
            temp <- one
        }
    }()
    return temp
}
func main() {
    se = lazy()
    for i := 0; i < 10; i++ {
        fmt.Println(f())
    }
}

Go语言函数参数传递。

package main
import (
    "fmt"
)
//值传递, 参数为切片
func modify3(a []int) {
    a = []int{3, 4}
}

//引用传递, 参数为切片
func modify4(a []int) {
    a[0] = 3
}

func main() {
    var arr []int = []int{1, 2}
    fmt.Println(arr)

    modify3(arr)
    fmt.Println(arr)

    modify4(arr)
    fmt.Println(arr)
}
package main
import (
    "fmt"
)
//自定义类型
type MyType struct {
    a int
}
//值传递
func modify1(myType MyType) {
    myType.a = 1
}
//引用传递
func modify2(myType *MyType) {
    myType.a = 1
}

func main() {
    myType := MyType{0}

    fmt.Println(myType.a)

    modify1(myType)
    fmt.Println(myType.a)

    modify2(&myType)
    fmt.Println(myType.a)
}
package main
import(
    "fmt"
)
//定义可选参数
type Options struct {
    one string
    two int
    three bool
}
//使用struct来聚合可变参数
func f1(a int, options Options){
    fmt.Println(options.one)
    fmt.Println(options.two)
    fmt.Println(options.three)
}

//使用interface{} 接口来统一变量类型
func f2(a int, values ...interface{}) {
    for _, v := range values {
        switch v.(type) {
        case string:
            fmt.Println("string: ", v.(string))
        case int:
            fmt.Println("int: ", v.(int))
        case bool:
            fmt.Println("bool: ", v.(bool))
        }
    }
}

func main() {
    fmt.Println("f1: ")
    f1(3, Options{one:"haha", two:10, three:true })
    fmt.Println("f2:")
    f2(3, "haha", 10, true)
}
/* 函数的调用方式
1. 普通函数
2. 函数闭包
3. 匿名函数
4. 方法调用
*/
package main
import (
    "fmt"
)

type MyType int
func (myType MyType)f() {
    fmt.Println("方法函数的调用")
}

//普通函数
func f() {
    fmt.Println("普通函数")
}

func main() {
    f();
    //函数闭包x
    x := func(){
        fmt.Println("闭包调用")
    }
    //用闭包名调动
    x()
    //匿名函数的调用
    //()代表调用
    func() {
        fmt.Println("匿名函数调用") 
    }()
    //定义类型变量,进行方法调用
    var myType MyType
    myType.f()
}
package main
import (
    "log"
)
type ByteSlice []byte
//指针和值传递
func(slice ByteSlice) Append1(data []byte)[]byte {
    slice = append(slice, data...)
    log.Println("in Append1 func: ", string(slice))
    return slice
}

func (slice *ByteSlice) Append2(data []byte) {
    lenSlice := len(*slice)
    lenNewSlice :=  lenSlice + len(data)
    newSlice := make([]byte, lenNewSlice)
    copy(newSlice[0:lenSlice], *slice)
    copy(newSlice[lenSlice:lenNewSlice], data)
    *slice = newSlice
}

func main() {
    var a  ByteSlice
    a = []byte("hello")
    b := a.Append1([]byte(" world1"))
    log.Println("main a: ", string(a))
    log.Println("main b: ", string(b))

    log.Println("a: ", string(a))
    a.Append2([]byte("world2"))
    log.Println("a: ", string(a))
}

猜你喜欢

转载自blog.csdn.net/wangwenhao00/article/details/78811940