英文:
Change hex to string
问题
我正在进行Golang教程,我现在在这一部分:
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("My favorite number is", rand.Seed)
}
这会返回My favorite number is 0xb1c20
我已经阅读了https://golang.org/pkg/math/rand/#Seed,但我仍然有些困惑,如何将其显示为字符串而不是十六进制。
英文:
I'm going through the Golang tutorial, and I am on this part
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("My favorite number is", rand.Seed)
}
This returns My favorite number is 0xb1c20
I have been reading on https://golang.org/pkg/math/rand/#Seed but I'm still a bit confused as to how have it instead of show the hex show a string
答案1
得分: 1
math/rand.Seed
是一个函数;你正在打印该函数在内存中的位置。你可能想要做类似以下的操作:
package main
import (
"fmt"
"math/rand"
)
func main() {
rand.Seed(234) // 用你的种子值替换,或者根据当前时间设置种子
fmt.Println("我的最喜欢的数字是", rand.Int())
}
英文:
math/rand.Seed
is a function; you are printing the function's location in memory. You probably meant to do something like the following:
package main
import (
"fmt"
"math/rand"
)
func main() {
rand.Seed(234) // replace with your seed value, or set the seed based off
// of the current time
fmt.Println("My favorite number is", rand.Int())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论