导入的结构方法不起作用

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

Imported struct method not working

问题

如果我运行以下代码,一切都可以编译和正常运行:

package main

import "fmt"

type Point struct {
    x, y int
}

func (p *Point) init() bool {
    p.x = 5
    p.y = 10
    return true
}

func main() {
    point := Point{}
    point.init()
    fmt.Println(point)
}

但是,当我将Point struct移动到$GOPATH目录下的一个包中时,我会得到以下错误:point.init undefined (cannot refer to unexported field or method class.(*Point)."".init)

有人能解释一下为什么会发生这种情况吗?

一旦我将Point struct放在名为class的包中,代码如下(错误出现在第8行,我调用init方法的地方):

package main

import "fmt"
import "class"

func main() {
    point := class.Point{}
    point.init()
    fmt.Println(point)
}
英文:

If I run the following code everything compiles and runs fine:

package main

import "fmt"

type Point struct {
	x, y int
}

func (p *Point) init() bool {
	p.x = 5
	p.y = 10
	return true
}

func main() {
	point := Point{}
	point.init()
	fmt.Println(point)
}

But when I move the Point struct to a package in the $GOPATH directory then I get the following error: point.init undefined (cannot refer to unexported field or method class.(*Point)."".init)

Can anyone explain to me why this happens?

Once I put the Point struct in a package called class the code looks as follows (the error is on the 8th line where I call the init method):

package main

import "fmt"
import "class"

func main() {
	point := class.Point{}
	point.init()
	fmt.Println(point)
}

答案1

得分: 12

init()重命名为Init()应该可以工作!
基本上,所有以非Unicode大写字母开头的函数、方法、结构体和变量只能在它们所在的包内可见!

你需要从语言规范中更多地了解这方面的内容:
http://golang.org/ref/spec#Exported_identifiers

相关部分:

> 为了允许从另一个包中访问标识符,可以将其导出。如果满足以下两个条件,则标识符被导出:
>
> 1. 标识符名称的第一个字符是一个Unicode大写字母(Unicode类别“Lu”);
> 2. 标识符在包块中声明,或者它是字段名或方法名。
其他所有标识符都不会被导出。

英文:

Rename init() to Init() should work!
Basically, all the things(function, method, struct, variable) that not start with an Unicode upper case letter will just visible inside their package!

You need to read more from the language spec here:
http://golang.org/ref/spec#Exported_identifiers

Relevant bit:

> An identifier may be exported to permit access to it from another package. An identifier is exported if both:
>
> 1. the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
> 2. the identifier is declared in the package block or it is a field name or method name.
All other identifiers are not exported.

答案2

得分: 4

只有函数/方法的名称的第一个字母大写的才会被导出。

http://golang.org/doc/effective_go.html#commentary

> 程序中的每个被导出的(首字母大写的)名称...

当我将init改为Init时,一切都正常工作了。

英文:

Only functions/methods that have the first letter of their name capitalized are exported

http://golang.org/doc/effective_go.html#commentary

> Every exported (capitalized) name in a program...

When I changed init to Init everything worked.

huangapple
  • 本文由 发表于 2013年12月9日 10:44:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/20462229.html
匿名

发表评论

匿名网友

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

确定