结构嵌入:无法引用未导出的名称os.fileStat

一段时间以前,我试图嵌入this answer中的以下提示。

// Has all methods of *sql.Row.
type myRow struct {
    *sql.Row
}

但是,当我尝试https://play.golang.org/p/Vnyx4lTwISn时会引发错误cannot refer to unexported name os.fileStat

package main

import (
    "fmt"
    "os"
)

type myfilestat struct {
    info *os.fileStat
}


func main() {
    fmt.Println("Hello,playground")
}

能给我指出正确的方向吗?

wlst34 回答:结构嵌入:无法引用未导出的名称os.fileStat

在运行中,以小写字母开头的标识符不可见。因此,出现“未导出的名称os.fileStat”错误。 参见https://golang.org/ref/spec#Exported_identifiers

听起来像您在追求os.Stat和os.LStat之类的东西。它们返回os包内部从fileStat派生的FileInfo类型。

https://github.com/golang/go/blob/a38a917aee626a9b9d5ce2b93964f586bf759ea0/src/os/stat_unix.go处查看unix os.Stat的实现

,

在Go中,当标识符的首字母大写时,该标识符对于要使用它的任何代码段都是公共的。

首字母小写时,标识符是私有的,只能在声明的包中访问。

Exported/Unexported Identifier blog post

本文链接:https://www.f2er.com/3104842.html

大家都在问