• go语言基础


    一、go语言关键字:

    1、常用关键字

    break default func interface select case defer go map struct chan else goto 
    const fallthrough if range type continue for import return var package switch

    2、占位符

    指针
    %p 十六进制表示,前缀 0x Printf("%p", &people) 0x4f57f0
    %v 相应值的默认格式。 Printf("%v", people) {zhangsan}
    %T 相应值的类型的Go语法表示 Printf("%T", people) main.Human
    %t true 或 false。 Printf("%t", true) true
    %d 十进制表示 Printf("%d", 0x12) 18
    %s 输出字符串表示(string类型或[]byte) Printf("%s", []byte("Go语言")) Go语言

    3、指针

    package main
    
    import "fmt"
    
    func main() {
       //指针取值
       a := 10
       var b = &a // 取变量a的地址,将指针保存到b中
       fmt.Printf("type of b:%T
    ", b)
       c := *b // 指针取值(根据指针去内存取值)
       fmt.Printf("type of c:%T
    ", c)
       fmt.Printf("value of c:%v
    ", c)
    }
    
    /*
    输出:
    type of b:*int
    type of c:int
    value of c:10
     

    总结: 取地址操作符&和取值操作符*是一对互补操作符,&取出地址,*根据地址取出地址指向的值。变量、指针地址、指针变量、取地址、取值的相互关系和特性如下

    1、 对变量进行取地址(&)操作,可以获得这个变量的指针变量。

    2、 指针变量的值是指针地址。

    3、 对指针变量进行取值(*)操作,可以获得指针变量指向的原变量的值。

    二、变量

    1、函数体外用var关键字进行全局变量的声明
    2、type关键字进行结构(struct)或是接口(interface)的声明

    三、函数

    函数可见性规则
    1、函数名小写开头即为private方法
    2、函数名大写开头即为public方法

    四、数组:

    1、数组的定义:var 数组变量名 [元素数量]T

    例1:var a [3]int

    数组可以通过下标进行访问,下标是从0开始,最后一个元素下标是:len-1,访问越界(下标在合法范围之外),则触发访问越界,会panic

    2、初始化

    func main() {
    var testArray [3]int
    var numArray = [...]int{1, 2}
    var cityArray = [...]string{"北京", "上海", "深圳"}
    fmt.Println(testArray) //[0 0 0]
    fmt.Println(numArray) //[1 2]
    fmt.Printf("type of numArray:%T
    ", numArray) //type of numArray:[2]int
    fmt.Println(cityArray) //[北京 上海 深圳]
    fmt.Printf("type of cityArray:%T
    ", cityArray) //type of cityArray:[3]string
    }

    3、数组的遍历

    2种:

    func main() {
    var a = [...]string{"北京", "上海", "深圳"}
    // 方法1:for循环遍历
    for i := 0; i < len(a); i++ {
    fmt.Println(a[i])
    }
    
    // 方法2:for range遍历
    for index, value := range a {
    fmt.Println(index, value)
    }
    }
  • 相关阅读:
    Flask上下文管理及源码刨析
    Python数据库连接池DBUtils
    装饰器的修复wraps,偏函数partial 以及chain
    unity官方案例精讲(第三章)--星际航行游戏Space Shooter
    c#多态性
    C# 继承
    c#类(class)
    一、事件函数的执行顺序(脚本的生命周期)
    一、Vuforia_AR
    四、其它(一)
  • 原文地址:https://www.cnblogs.com/wuchangblog/p/14106106.html
Copyright © 2020-2023  润新知