我有以下包裹:
package mypkg
type (
// mystruct ...
mystruct struct {
S string
}
)
// New ..
func New() *mystruct {
return &mystruct{S: "test"}
}
我这样使用它:
package main
import (
"fmt"
"test/mypkg"
)
func main() {
x := mypkg.New()
fmt.Println(x.S)
// this fails intended
y := mypkg.mystruct{S: "andre"}
fmt.Println(y.S)
}
为什么 golint 抱怨我未导出的结构?我的意图是防止在构造函数调用之外调用结构创建。是否有另一种方法可以防止在没有 New 调用的情况下实例化?
郎朗坤
三国纷争
随时随地看视频慕课网APP
相关分类