zoukankan      html  css  js  c++  java
  • go 基本语法

    package main

    import "fmt"
    import "time"

    func main() {

    //声明变量
    var param_a int
    var param_b, param_c int

    param_a, param_b, param_c = 1, 1, 1
    fmt.Println(param_a, param_b, param_c)
    /* 定义局部变量 */
    var a int = 100
    var b int = 200
    var ret int
    /* 临时变量 */
    test := "test"
    fmt.Println(test)

    /* 调用函数并返回最大值 */
    ret = max(a, b)

    fmt.Printf( "最大值是 : %d ", ret )

    d, e := swap("Google", "Runoob")
    fmt.Println(d, e)

    /* 数组 */
    var balance = [10] int {1, 2, 3, 4}
    fmt.Println(balance)
    fmt.Println(balance[2])

    /* 循环 */
    var i int
    for i = 0; i< 10 ; i++ {
    fmt.Println(balance[i]);
    }

    /* 指针 */
    var f int = 10
    var ip *int

    //内存地址
    fmt.Println(&f);
    ip = &f
    fmt.Println(ip)
    fmt.Println(*ip)

    /* 结构体 */
    type Books struct {
    title string
    author string
    subject string
    book_id int
    }

    var book1 Books
    book1.title = "python"
    book1.author = "python"
    book1.title = "python"

    fmt.Println(Books{"Go 语言", "www.runoob.com", "Go 语言教程", 6495407})
    fmt.Println(Books{title:"python", author:"www.python.com", subject:"python 语言编程", book_id:222222})
    fmt.Println(book1.title)

    /**语言切片**/ //相对数组来说, 切片的长度是可变的,切片是(动态的数组)

    //数组
    var balance_2 = [10]int{1, 2} //var variable_name [variable_length] variable_type
    fmt.Println(balance_2)

    //多维数组
    var balance_3 = [2][3]int {{1, 2, 3}, {2, 2, 3}}
    fmt.Println(balance_3)
    fmt.Println(balance_3[0][0])

    //切片
    //var identifier []type 或者 make([]type, len)
    //

    var param1 = []int {1, 2, 3}

    fmt.Println(param1)

    /* 循环数组*/

    for i,j := range(balance_3) {
    fmt.Println(i, j);
    for x,y := range(j) {
    fmt.Println(x,y)
    }
    }

    //数组长度
    fmt.Println(len(balance_3))
    fmt.Println(cap(balance_3))

    //map 是一对无序的键值对 声明 var map_variable map[map_key_type] map_value_type

    var map_param_1 map[string]string
    map_param_1 = make(map[string]string)
    map_param_1 ["name"] = "wxdr";
    map_param_1 ["age"] = "10"

    fmt.Println(map_param_1["name"])

    // map 数据删除
    delete (map_param_1, "name")
    for itme,value := range(map_param_1) {
    fmt.Println(itme, value)
    }

    //数据类型转换 variable_type expression
    var param2 int = 2
    var param3 int = 9
    var res float32
    res = float32 (param2) / float32(param3)
    fmt.Println(res)

    //并发 go 函数名(参数列表) 允许开启一个新的运行期线程
    go say("hello")
    say("world")

    //通道 channel 作用是, 两个 goroutine (轻量级线程) 之间通讯
    //ch <- v (把v的值发送给通道 ch)
    //v := <- ch (从ch 接收数据, 并将其复制给 v)
    //申请通道 ch := make(chan int)

    ch := make(chan int)

    var parma4 int = 11
    go showC(parma4, ch)
    go showC(parma4, ch)
    a_b := <-ch
    fmt.Println(a_b)
    }

    /* 函数返回两个数的最大值 */
    func max(num1, num2 int) int {
    /* 声明局部变量 */
    var result int

    if (num1 > num2) {
    result = num1
    } else {
    result = num2
    }
    return result
    }

    func swap(x, y string) (string, string) {
    return y, x
    }

    func say(s string) {
    for i := 0; i < 5; i++ {
    time.Sleep(100 * time.Millisecond)
    fmt.Println(s)
    }
    }

    func showC(parma int, c chan int) {
    c <- (parma - 1)
    }
  • 相关阅读:
    .NET实现Excel文件的读写 未测试
    权限管理设计
    struts1中配置应用
    POJ 2139 Six Degrees of Cowvin Bacon(floyd)
    POJ 1751 Highways
    POJ 1698 Alice's Chance
    POJ 1018 Communication System
    POJ 1050 To the Max
    POJ 1002 4873279
    POJ 3084 Panic Room
  • 原文地址:https://www.cnblogs.com/wxdr/p/14598470.html
Copyright © 2011-2022 走看看