Function 缺失功能体
当我在Golang中看到Function 缺失功能体,function,go,Function,Go,当我在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 { retu
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)
}
我有一个错误,说缺少“Generate”的函数体
那么我如何编写像math.Acos
这样的函数呢?Acos
和Acos
是不同的函数,具有不同的实现。您的Generate()
和Generate()
也是如此
acos
方法是在汇编中实现的,这只是方法原型。您不需要预先声明UUID生成函数,因为编译器是多过程的。我发现,如果您的函数有多个返回值,并且您没有使用括号换行,例如func-Somefunc(测试输入)字符串,error
在使用括号换行func-Somefunc(测试输入)(字符串,error)之后,您将得到此错误
错误消失。
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
}