当我在Golang中看到math.Sin
时,我想知道因为它是两个具有相同名称的函数但是第一个函数没有函数体。
见下文:
例如math.Acos
:
// Acos returns the arccosine, in radians, of x.
//
// Special case is:
// Acos(x) = NaN if x < -1 or x > 1
func Acos(x float64) float64
func acos(x float64) float64 {
return Pi/2 - Asin(x)
}
但是当我想用以下语句创建一个非常简单的uuid
包时:
package uuid
import (
"crypto/rand"
"fmt"
"io"
)
// Generate generates a random UUID according to RFC 4122
func Generate() (string, error)
func generate() (string, error) {
uuid := make([]byte, 16)
n, err := io.ReadFull(rand.Reader, uuid)
if n != len(uuid) || err != nil {
return "", err
}
uuid[8] = uuid[8]&^0xc0 | 0x80
uuid[6] = uuid[6]&^0xf0 | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}
我收到了missing function body for "Generate"
那么如何编写math.Acos
等函数?
答案 0 :(得分:1)
acos
和Acos
是不同的功能,具有不同的实现。与您的Generate()
和generate()
相同。
acos
方法在程序集中实现,这只是方法原型。您不需要预先声明您的UUID生成函数,因为编译器是多遍的。