• GO基础知识(基础数据类型之字符串、常量)


    go 语言注意事项

    /////////////字符串
    /**
    字符串值不可变
    **/
    ex:
    s := "left foot"
    t := s
    s += ", right foot"
    fmt.Println(s) //"left foot, right foot"
    fmt.Println(t) //"left foot"
    
    
    //字符串面值,是用双引号括起来的
    "Hello, 世界"
    //一个原生的字符串面值形式是`...`,使用反引号代替双引号。
    //且字符串内没有转义操作。
    const GoUsage = `Go is a tool for managing Go source code.
    
    Usage:
        go command [arguments]
    ...`
    
    
    //字符串和数字的转换
    x := 123
    y := fmt.Sprintf("%d", x) //格式化返回字符串
    fmt.Println(y, strconv.Itoa(x)) // "123 123"  //用strconv.Itoa()
    
    //字符串解析为数字
    x, err := strconv.Atoi("123")             // x is an int
    y, err := strconv.ParseInt("123", 10, 64) // base 10, up to 64 bits
    
    
    ///////////////常量
    // 常量表达式的值在编译期计算,而不是在运行期,每种常量的潜在类型都是基础类型:boolean、string或数字
    //常量不可修改
    //一个常量的声明也可以包含一个类型和一个值,但是如果没有显式指明类型,那么将从右边的表达式推断类型。
    const noDelay time.Duration = 0
    const timeout = 5 * time.Minute //time.Minute是time.Duration类型的常量
    fmt.Printf("%T %[1]v
    ", noDelay)     // "time.Duration 0"
    fmt.Printf("%T %[1]v
    ", timeout)     // "time.Duration 5m0s"
    fmt.Printf("%T %[1]v
    ", time.Minute) // "time.Duration 1m0s"
    //批量声明常量,除了第一个其余均可省略
    const (
        a = 1
        b
        c = 2
        d
    )
    fmt.Println(a, b, c, d) // "1 1 2 2"
    
    
    // iota常量生成器
    type Weekday int
    
    const (
        Sunday Weekday = iota //初始枚举值是0
        Monday //值为1
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
    )
    
    
    // 无类型常量
    //有六种未明确类型的常量类型,分别是无类型的布尔型、无类型的整数、无类型的字符、无类型的浮点数、无类型的复数、无类型的字符串。例如0、0.0、0i和u0000虽然有着相同的常量值,但是它们分别对应无类型的整数、无类型的浮点数、无类型的复数和无类型的字符等不同的常量类型。同样,true和false也是无类型的布尔类型,字符串面值常量是无类型的字符串类型。
    
    //注意有一点不同:无类型整数常量转换为int,它的内存大小是不确定的,但是无类型浮点数和复数常量则转换为内存大小明确的float64和complex128。
    
    
  • 相关阅读:
    Notes of the scrum meeting(12.7)
    Notes of the scrum meeting(12.5)
    事后分析报告(M1阶段)
    锁屏软件发布说明
    Android 锁屏软件MemoryDebris测试报告
    锁屏软件功能介绍
    Notes of the scrum meeting(11/4)
    Notes of the scrum meeting(11/3)
    Notes of the scrum meeting(11/2)
    SCRUM 12.17
  • 原文地址:https://www.cnblogs.com/michealjy/p/14457262.html
Copyright © 2020-2023  润新知