gpt4 book ai didi

import - 使用 Go 包

转载 作者:IT王子 更新时间:2023-10-29 01:24:58 28 4
gpt4 key购买 nike

我不确定如何调用 Go 包。例如,如果我想创建随机数,我应该导入 "math/random",但它不只是 "math" 库的一部分吗?那么为什么这段代码不起作用:

package main

import(
"fmt"
"math"
)

func main(){
r := rand.New(rand.NewSource(99))
fmt.Println(r)

}

我的意思是,我不能通过简单地导入一个父类(super class)(在这种情况下,只是数学 "math" 包)直接访问随机函数吗?

最佳答案

那是因为rand是一个单独的包,在层次上是在mathmath/rand之下,所以你要专门导入它:

package main

import(
"fmt"
"math/rand"
)

func main(){
r := rand.New(rand.NewSource(99))
fmt.Println(r)

}

关于import - 使用 Go 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27645670/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com