走进 Go 语言基础语法 | 青训营

摘要
Title: 走进 Go 语言基础语法 | 青训营
Tag: ByteDance、Go、后端

Powered by:NEFU AB-IN

走进 Go 语言基础语法 | 青训营

  • Go 语言入门指南:基础语法和常用特性解析
  • go.mod

    go.mod 这个文件里记录了当前项目里所有依赖包的 git 仓库地址以及对应的版本号,来解决了包依赖管理的问题,后续在构建编译时,就会根据对应的版本号去拉取依赖包。

    注意, 如果当前的项目是要给外部使用的,最好是配合 git 仓库命名,比如

    1
    go mod init github.com/lincoln/manage
    1
    2
    go get ... // 获取包
    go mod tidy // 将会扫描所有我们 import 到的包,并生成对应的记录到 gomod 文件里
  • 笔记代码注释

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    /*
    * @Author: NEFU AB-IN
    * @Date: 2023-08-06 09:44:15
    * @FilePath: \GoTest\1.go
    * @LastEditTime: 2023-08-06 22:57:59
    */
    package main

    import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    )

    var (
    in = bufio.NewReader(os.Stdin)
    out = bufio.NewWriter(os.Stdout)
    )

    func Read[T any]() T {
    var i T
    fmt.Fscan(in, &i)
    return i
    }

    const N int = 1e5 + 10

    // 函数
    func add(a int, b int) (v int, ok bool) {
    return a + b, ok
    }

    func add2(a *int) {
    *a += 2
    }

    // 结构体
    type sa struct {
    x int
    y int
    }

    // 结构体方法(类成员函数)
    func (a sa) add(x int) bool {
    return a.x == 2
    }

    func main() {
    defer out.Flush()

    // 1. variable
    a := "123"
    var b int = 2
    var c int64
    /*
    int 类型大小为 8 字节
    int8 类型大小为 1 字节
    int16 类型大小为 2 字节
    int32 类型大小为 4 字节
    int64 类型大小为 8 字节
    */
    var d float64

    e := a + "foo"

    const s = "1231"

    fmt.Println(a, b, c, d) // 带换行
    fmt.Print(s) // 不带换行
    fmt.Println(e)

    // 2. if else

    f := Read[int]()
    if f%2 == 0 {
    print("even")
    } else {
    print("odd")
    }

    // 3. 循环
    for i := 0; i < 10; i++ {
    fmt.Print("!")
    }

    // 4. 数组 两种声明方式
    var g [N]int
    var h = [N]int{}
    fmt.Println(g[1:2], h[2:3])

    // 5. slice 切片
    j := make([]string, 3)
    j = append(j, "cc")
    fmt.Println(j[2:5])

    // 6. map
    mp := map[string]int{}
    mp["xw"] = 100
    mp["lsy"] = 12
    l, ok := mp["xw"]
    fmt.Println(l, ok)

    delete(mp, "xw")
    for key, val := range mp {
    fmt.Println(key, val)
    }

    // 7. 给构体
    m := sa{x: 10}
    fmt.Println(m)

    // 8. 格式化
    fmt.Printf("f=%v\n", f)
    fmt.Printf("f=%+v\n", m)
    fmt.Printf("f=%#v\n", m)

    // 9. 转换
    y := 10
    z := "123"

    fmt.Println(strconv.Atoi(z))
    fmt.Println(strconv.Itoa(y))

    }

    另外字符串和数字互相转换的例子
    image.png

  • JSON处理

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    package main

    import (
    "encoding/json"
    "fmt"
    )

    type Student struct {
    Name string `json:"name"` // 字段名的第一个字母大写
    Sid string `json:"sid"`
    }

    func main() {
    s := Student{Name: "jonh" ,Sid: "10323"}
    //序列化
    p ,err := json.Marshal(s) // 序列化
    if err!=nil {
    panic(err)
    }
    fmt.Println(string(p)) // 不加string的话就是一些16进制的编码
    // {"name":"jonh","sid":"10323"} 因为加了tag,导致变为小写

    //反序列化
    err = json.Unmarshal(p,&s)
    if err!=nil {
    panic(err)
    }
    fmt.Printf("%#v\n", s) // main.Student{Name:"jonh", Sid:"10323"}
    }

  • 时间库的使用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    package main

    import (
    "fmt"
    "time"
    )

    func main() {
    now := time.Now() //获取当前时间
    fmt.Printf("current time:%v\n", now)
    year := now.Year() //年
    month := now.Month() //月
    day := now.Day() //日
    hour := now.Hour() //小时
    minute := now.Minute() //分钟
    second := now.Second() //秒
    fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)

    now1 := time.Now() //获取当前时间
    timestamp1 := now1.Unix() //时间戳
    timestamp2 := now1.UnixNano() //纳秒时间戳
    later := now1.Add(time.Hour) // 当前时间加1小时后的时间
    fmt.Printf("现在的时间戳:%v\n", timestamp1)
    fmt.Printf("现在的纳秒时间戳:%v\n", timestamp2)
    fmt.Println(later)
    }

  • CF代码模板

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    /*
    * @Author: NEFU AB-IN
    * @Date: 2023-08-06 22:57:54
    * @FilePath: \GoTest\tmp.go
    * @LastEditTime: 2023-08-06 23:05:28
    */

    package main

    import (
    "bufio"
    "fmt"
    "os"
    )

    var (
    in = bufio.NewReader(os.Stdin)
    out = bufio.NewWriter(os.Stdout)
    )

    func Read[T any]() T {
    var i T
    fmt.Fscan(in, &i)
    return i
    }

    const N int = 1e5 + 10

    func solve() {

    }

    func main() {
    defer out.Flush()
    T := Read[int]()
    for T > 0 {
    solve()
    T--
    }
    }

使用搜索:谷歌必应百度