将十六进制转换为字符串。

huangapple go评论90阅读模式
英文:

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())
}

huangapple
  • 本文由 发表于 2016年2月7日 03:47:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/35245642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定