• 【go学习笔记】二、变量、常量【连载】


    编写测试程序

    1. 源码文件以_test结尾;例如:xxx_test.go
    2. 测试方法名以Test开头;例如:func TestXXXXX(t *testing.T){}

    实现斐波那且数列

    package fib
    
    import (
        "fmt"
        "testing"
    )
    
    func TestFibList(t *testing.T) {
        // 第一种赋值方式
        //var a int =1
        //var b int =1
    
        // 第二种赋值方式
        //var (
        //    a int = 1
        //    b     = 1
        //)
    
        // 第三种赋值方式
        a := 1
        b := 1
        fmt.Print(a)
        for i := 0; i < 5; i++ {
            fmt.Print(" ", b)
            tmp := a
            a = b
            b = tmp + a
        }
        fmt.Println()
    }
    

    代码里边给出了三种赋值方式:

    one

    var a int =1 一般全局变量,或者外部变量
    

    two

    var (
        a int = 1
        b     = 1
    )
    

    three go语言可以不使用关键字,使用类型推断直接给变量设置类型

    a := 1
    

    变量赋值

    • 赋值可以进行自动类型推断
    • 在一个赋值语句中可以对多个变量进行同时赋值
    func TestExchang(t *testing.T) {
        a := 1
        b := 1
        // 1 常见写法
        //tmp := a
        //a = b
        //b = tmp
        //
        // 2 go 特性:多个变量进行同时赋值
        a, b = b, a
        t.Log(a, b)
    }
    

    两点需要注意

    1. 常见写法
      tmp := a
      a = b
      b = tmp
      
    2. go 特性:多个变量进行同时赋值
      a, b = b, a
      

    常量定义

    快速设置连续值

    const (
        Monday = iota + 1
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
        Sunday
    )
    
    const (
        Open = 1 << iota
        Close
        Pending
    )
    

    测试代码

    package constart_test
    
    import (
        "testing"
    )
    
    const (
        Monday = iota + 1
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
        Sunday
    )
    
    const (
        Open    = 1 << iota //0001
        Close               //0010
        Pending             //0011
    )
    
    func TestConstantTry(t *testing.T) {
        t.Log(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
    
    }
    
    func TestConstantTry1(t *testing.T) {
        a := 6
        t.Log(uint8(a), uint(Open), uint(Close), uint(Pending))
        c := a & Open
        t.Logf("第一行 - c 的值为 %d
    ", c)
        c = a & Close
        t.Logf("第一行 - c 的值为 %d
    ", c)
        c = a & Pending
        t.Logf("第一行 - c 的值为 %d
    ", c)
        t.Log(a&Open == Open, a&Close == Close, a&Pending == Pending)
    }
    

    输出

    --- PASS: TestConstantTry (0.00s)
        constant_try_test.go:24: 1 2 3 4 5 6 7
    === RUN   TestConstantTry1
    --- PASS: TestConstantTry1 (0.00s)
        constant_try_test.go:30: 6 1 2 4
        constant_try_test.go:32: 第一行 - c 的值为 0
        constant_try_test.go:34: 第一行 - c 的值为 2
        constant_try_test.go:36: 第一行 - c 的值为 4
        constant_try_test.go:37: false true true
    PASS
    
    Process finished with exit code 0
    
    示例代码请访问: https://github.com/wenjianzhang/golearning
  • 相关阅读:
    内存分配略谈
    变量声明顺序和指针偏移问题示例
    解决You have to be inside an Angular CLI project in order to use the serve command
    Linux中将命令放到全局
    Linux设置vim显示行号
    根据指定的excel模板导出数据
    解决 Maven工程运行报错Failed to clean project: Failed to delete
    springcloud工作用常用到的一些注解
    cmd窗口备份mysql数据库
    调用存储过程msql
  • 原文地址:https://www.cnblogs.com/zhangwenjian/p/11872961.html
Copyright © 2020-2023  润新知