struct 's field and method name can not be same? why?

2010 views
Skip to first unread message

Brain Wu

unread,
Sep 2, 2014, 11:24:40 AM9/2/14
to golan...@googlegroups.com
I have a article struct 
type Article struct{
Tags []Tag
}
func (a *Article) Tags() {
}
it's has a error that "type Article has both field and method named Tags" when i build it.
Why is  such a design?

Jan Mercl

unread,
Sep 2, 2014, 11:29:08 AM9/2/14
to Brain Wu, golang-nuts
Consider

var v Article
var u = v.Tags

What type is u? '[]Tag' or 'func(*Article)'?

-j

吴俊滨

unread,
Sep 2, 2014, 11:31:36 AM9/2/14
to Jan Mercl, golang-nuts
i got it. thank you very much

chris dollin

unread,
Sep 2, 2014, 11:36:19 AM9/2/14
to Brain Wu, golang-nuts
As a general rule [1], Go doesn't let you declare the same name more
than once in the same scope, so that if you have the name, you know
what thing it is.

Similarly Go doesn't allow function/method overloading.

Chris

[1] Apart from the redeclaration feature of :=, a topic of hot discussion.

--
Chris "allusive" Dollin
Reply all
Reply to author
Forward
0 new messages