关于go:Go-快速入门指南-包的导入

1次阅读

共计 458 个字符,预计需要花费 2 分钟才能阅读完成。

导入包

关键字 import

语法规定

  • 单个导入
import "包名"
  • 多个导入
import (
    "包名 1"
    "包名 2"
    "包名 3"
    ...
)
  • 导入包应用别名
import 别名 "包名"

例子

  • 导入 打印包
package main

import "fmt"

func main() {fmt.Println("hello world")
}
  • 导入 打印包 字符串包
package main

import (
    "fmt"
    "strings"
)

func main() {fmt.Println("hello world")
    fmt.Println(strings.Repeat("hello", 3)) // 字符串反复
}

// $ go run main.go
// 输入如下
/**
    hello world
    hello hello hello
*/
  • 导入包应用别名
package main

import (
    "fmt"
    myStr "strings"
)

func main() {fmt.Println(myStr.Repeat("hello", 3))
}

// $ go run main.go
// 输入如下
/**
    hello hello hello
*/

分割我

正文完
 0