美文网首页
1 - Go的基础知识

1 - Go的基础知识

作者: 天命_风流 | 来源:发表于2020-07-01 23:53 被阅读0次

测试

  • 源码文件为 xx_test.go
  • 测试方法名以 Test 开头
  • 使用 t.Log("xxx") ,可以在测试中输出内容
package try_test

import "testing"

func TestFirstTry(t *testing.T){
    t.Log("My first try!")
}

变量赋值方式

  • 变量有三种赋值方式
  • Go 中具有一定的类型推断能力
package fib

import (
    "testing"
)

func TestFbiList(t *testing.T)  {
    //var a int = 1
    //var b int = 1

    //a := 1
    //b := 1

    var (
        a int = 1  // 指定字符类型
        b     = 1  // 自动判断类型
    )

    for i := 0 ; i < 5 ; i++{  // 在这行代码中,使用 i := 0 对 i 进行了赋值
        t.Log(b)
        tmp := a
        a = b
        b = tmp + a
    }
}
  • 除此之外,Go 中有语法糖,可以快速实现变量
func TestExchange(t *testing.T){
    a := 1
    b := 2
    a, b = b, a
    t.Log(a, b)
}

常量赋值

  • 使用 const 进行常量赋值
  • iota 可以实现递增,你甚至可以用它做位移动
package constant_test

import (
    "reflect"
    "testing"
)

const (
    Monday = 10
    Tuesday = iota + 1
    Wednesday
)

const (
    Readable = 1 << iota // 0001
    Writable  // 0010
    Executable  // 0100
)

func TestContantTry(t *testing.T){
    t.Log(Monday, Tuesday, Wednesday)  // 10,2,3
}

func TestConstantTry1(t *testing.T){
    a := 7  // 0111
    t.Log(reflect.TypeOf(Readable))  // int
    t.Log(Readable, Writable, Executable)  // 1,2,4
    t.Log(a & Readable, a & Writable, a & Executable )  // 1,2,4
}

数据类型

  • 数据类型和其他的编程语言类似
  • Go 不允许任何类型的隐式类型转换
  • 甚至别名和原有类型之间都无法进行转换
package type_test

import (
    "math"
    "testing"
)

type MyInt int64

func TestImplicit(t *testing.T){
    var a int = 1
    var b int64 = 64
    //b = a  这行代码无法被被编译通过
    b = int64(a)
    t.Log(a, b)
    var c MyInt
    //c = b  这行代码也无法编译
    c = MyInt(b)
    t.Log(b, c)
}
  • Go 支持指针,但是不允许进行地址级别的运算
func TestPoint(t *testing.T){
    a := 1
    aPtr := &a
    //aPtr = aPtr + 1  无法编译
    t.Log(a, aPtr)  // 1,0xc00008a198
    t.Logf("%T %T", a, aPtr)  // int,*int
}
  • Go 中会有内置的一些值
func TestMaxInt(t *testing.T){
    t.Log(math.MaxInt16)  //32767
}
  • Go 的字符串初始化为空字符串,而不是空
  • Go 的数字会被初始化为 0,而不是空
func TestString(t *testing.T)  {
    var s string
    var i int
    t.Log("-" + s + "-")  // --
    t.Log(len(s))  // 0
    if s == ""{
        t.Log("none")  // 输出 none ,说明 s 就是一个空字符串,而非一个空指针
    }
    t.Log(i)  // 0
}

运算符

  • 常见的算数运算符号:+、-、*、/、%、++、--
  • 比较运算符中, == 判断数组有两个限制:长度 和 维数必须相同
package operatro_test

import "testing"

const (
    Readable = 1 << iota
    Writable
    Executable
)

func TestCompareArray(t *testing.T){
    a := [...]int{1,2,3}
    b := [...]int{1,2,3}
    c := [...]int{1,4,3}
    //d := [...]int{1,2,3,4}
    t.Log(a == b)
    t.Log(a == c)
    //t.Log(a == d)  在 Go 中,这行代码无法编译
}
  • 逻辑运算符:&&,||,!
  • 位运算符:&,|,,<<,>>,特别地,& 为按位置零:右边为 1,必定置零,右边为 0,结果为左边
func TestBitClear(t *testing.T)  {
    a := 7  // 0111
    a = a &^ Readable  // 0110
    a = a &^ Writable  // 0100
    t.Log(a) // 4
    t.Log(Readable, Writable, Executable) // 1 2 4
    t.Log(a & Readable, a & Writable, a & Executable) // 0 0 4
}

条件语句

  • if 的条件必须为 bool 类型
  • if 可以做一个初始赋值操作,这个操作在 Go 中其实有很多用处,在之后你会感受到它的好处
package condition

import "testing"

func TestIfMultSec(t *testing.T)  {
    if a := true; a{
        t.Log("OK", )
    }

    if _, err := true,true; err == true{
        t.Log("True")
    }
}
  • switch 后面可以接多个条件,使用逗号作为分隔
  • 每个 case 后会自动 break,所以你不需要手动实现
func TestSwitch(t *testing.T){
    for i := 0; i < 5; i++{
        switch i{
        case 1, 3:
            t.Log("Odd")
        case 0, 2:
            t.Log("Even")
        default:
            t.Log("UnKnow")
        }

        switch {
        case i % 2 == 1:
            t.Log("Odd")
        case i % 2 == 0:
            t.Log("Even")
        default:
            t.Log("UnKnow")
        }
    }
}

循环语句

  • 所有的循环都是使用 for 完成,你可以将它当成 while
package loop

import "testing"

func TestWhileLoop(t *testing.T)  {
    n := 1
    for n < 5 {
        t.Log(n)
        n++
    }
    for i := 1 ; i <= 5 ; i++ {
        t.Log(i)
    }
    for {
        t.Log('h')  // 这是一个死循环
    }

}

相关文章

  • go基础

    go 语言基础知识汇总

  • 1 - Go的基础知识

    测试 源码文件为 xx_test.go 测试方法名以 Test 开头 使用 t.Log("xxx") ,可以在测试...

  • Go基础知识整理

    go语言基础知识整理 1.历史拒谈,只记录属于go的知识2.安装环境可以看官网 正文 1.小知识:本地浏览器打开g...

  • Golang学习笔记4——控制语句

    1.基础知识补充(指针、递增递减语句) 指针go虽然保留了指针,但是与其它编程语言不同的是,在go当中不支持指针运...

  • Golang学习笔记

    1. Go开发环境搭建 2. Go基础知识 3. 类型与变量 4. 常量与运算符 5. 控制语句 6. 数组arr...

  • Go管道初识

    Go管道初识 Go管道基础知识 管道分类 无缓冲(unbuffered channel)无缓冲的通道是指在接收前没...

  • go内存管理

    1. Go 内存的划分 强烈推荐参考链接 在讲Go的堆栈之前,先温习一下堆栈基础知识。 什么是堆栈?在计算机中堆栈...

  • 二. Go(Go基础知识)

    一. Go基础语法 变量的定义 使用var关键字,可以放在函数内,或者直接放在包内,使用var()集中定义变量,使...

  • Go+MySQL考核总结

    Go+MySQL阶段考核总结 阶段时间:2021年8月12日~2021年9月16日 Golang基础知识博客:go...

  • Go的基础知识

    go语言学习笔记 一. 基本结构,基本数据类型 1. 文件名、关键字与标识符 Go 的源文件以 .go 为后缀名存...

网友评论

      本文标题:1 - Go的基础知识

      本文链接:https://www.haomeiwen.com/subject/pcfxfktx.html