• golang入门关键字以及简单用法说明


    golang只有25个关键字

    package: 定义包名, go中任何一个文件必须有一个package, 一般而言,package的定义和文件所属文件夹一致, 并且main函数所在文件的package必须是main

    import: 导入包名的关键字

    const: 声明常量的关键字

    var: 声明变量的关键字

    func: 定义函数关键字(结构体方法其本质也是函数)

    defer: 延迟执行(先注册后执行)关键字(常用于保证资源的释放). defer后面必须是函数或者方法的调用.osExit()主动调用时,defer不再执行.最好不要对有名返回值操作.

    go: 并发关键字

    return: 函数返回

    struct: 定义结构体

    interface: 定义接口(所有变量都实现空接口)

    map: 声明创建map类型关键字

    chan: 声明创建chan类型关键字

    以及一些控制流程的关键字

    if, else, for, range, break, continue

    swich, select, case, fallthrough, default

    type: 这个关键字非常常用, 定义结构体,类型等

    goto: 跳转语句关键字

    if else: 控制结构

    //-----------------------------------------------------------------------------------------------------------------

    package main

    import (
    "fmt"
    "time"
    )

    var testVar1 = 0 //自动识别类型
    var testChanVar chan *user

    const (
    testIota1 int = iota //初始化为0
    testIota2 //不写表达式 视为上一个表达式, 等价于 testIota2 int = iota
    testIota3 int = 9
    testIota4
    testIota5 int = iota //iota只有出了所属的const() 才重置为0
    )

    type user struct {
    name string
    age int
    }

    func testMap() {
    fmt.Printf("this is testMap... ")
    tMap := make(map[string]interface{}, 4) //value的类型是空接口类型, 所有类型都实现了空接口, 所以value的值可以是任意类型.
    tMap["name"] = "xzk"
    tMap["age"] = 25
    for key, value := range tMap {
    fmt.Printf("key=%v, value=%v ", key, value) //无序, 每次打印的顺序不一样
    switch key { //switch 后可以没有变量, 那么case可以是表达式,相当于if else
    case "name":
    fmt.Printf("this is swith case name... ")
    fallthrough //上一个case命中后, 强制执行下一个case(不管一下个case是否命中)
    case "fallthrough":
    fmt.Printf("this is fallthrough... ")
    default:
    fmt.Printf("this is default... ")
    }
    }
    }

    func pushChan() {
    fmt.Printf("this is pushChan... ")
    userBean := &user{
    name: "xzk",
    age: 18,
    }

    select {
    case testChanVar <- userBean: //如果通道未满, 将数据放入通道
    default:
    fmt.Printf("testChanVar chan overflow, size=%d, data=%v ", cap(testChanVar), userBean)
    }
    }

    func getChanData() {
    fmt.Printf("this is getChanData start... ")
    testChanVar = make(chan *user, 1000) //初始化大小为1000的通道
    for userBean := range testChanVar { //遍历通道, 取出数据
    fmt.Printf("this is getChanData..., userBeanName=%s ", userBean.name)
    }
    }

    func init() {
    go getChanData() //异步执行getChanData()函数
    fmt.Printf("this is init end... ")
    }

    func main() {
    fmt.Printf("this is main start... ")
    defer fmt.Printf("this is defer... ") //多个defer执行顺序:先进后出
    // 使用时需要注意 引用变量和值变量 的区别(return语句实际上也是分2个步骤. 1:设置返回值, 2:真正的return. defer是在中间执行)

    go pushChan()

    go testMap()

    for {
    fmt.Printf("this is main...%d ", testIota5)
    time.Sleep(time.Second)
    testVar1++
    if testVar1 == testIota5 {
    break
    }
    }

    fmt.Printf("this is main end... ") //main函数退出, go异步线程随之结束
    }

    //------------控制台打印结果---------------------------------------

    this is init end...
    this is getChanData start...
    this is main start...
    this is pushChan...
    this is main...4
    this is getChanData..., userBeanName=xzk
    this is testMap...
    key=age, value=25
    this is default...
    key=name, value=xzk
    this is swith case name...
    this is fallthrough...
    this is main...4
    this is main...4
    this is main...4
    this is main end...
    this is defer...

  • 相关阅读:
    交叉熵的数学原理及应用——pytorch中的CrossEntropyLoss()函数
    pytorch中如何使用DataLoader对数据集进行批处理
    Pytorch中的自动求导函数backward()所需参数含义
    Pytorch中的torch.cat()函数
    Pytorch中的squeeze()和unsqueeze()函数
    UBUNTU18.04安装网易云音乐并直接图标启动
    UBUNTU18.4环境下使用更好用的搜索引擎(无奈,只能起这样的标题)
    Ubuntu 18.04换国内源 中科大源 阿里源 163源 清华源
    共享栈
    C++(十七) — 宏代码、内联函数
  • 原文地址:https://www.cnblogs.com/-xuzhankun/p/10502793.html
Copyright © 2020-2023  润新知