24

go语言的基础知识

 4 years ago
source link: https://segmentfault.com/a/1190000022142947
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

1、go基本数据类型,整型、浮点、布尔、字符(string)

1、双引号, 会识别转义字符

2、反引号,以字符串的原生形式输出,包括换行和特殊字符,可以实现防止攻击、输出源代码等效果

package main

import (
    "fmt"
)
func main() {
    var str1 = "abc\nabc"
    fmt.Println(str1)
    str2 := `
    package main
    import (
        "fmt"
    )
    func hello() {
        var n1 float32 = -123.0000089
        fmt.Println(n1)
    }`
    fmt.Println(str2)
}

3、当一行字符串太长时,需要使用到多行字符串,可以分行写,+号必须保留在上一行

4、Go 在不同类型的变量之间赋值时需要显式转换,在转换时,需要考虑范围,比如将int64 转成int8 【-128---127】,编译时不会报错,只是转换的结果是按溢出处理.

zMB3aqB.png!web

5、基本类型转string 类型

方式1:fmt.Sprintf("%参数", 表达式)

YFJ773b.png!web

方式2:使用strconv 包的函数(FormatBool, FormatInt, FormatFloat, Itoa)

6、string 类型转基本数据类型 ()

strconv.ParseInt 、 ParseFloat、 ParseBool


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK