Addition to struct tutorial


(Mike) #1

Importance of letter case should be covered more broad and addressed throughout. It’s very vague and since there isn’t an explicit reserved word for this function, it needs to be crystal clear to those new to the language. Go comes across as inconsistent otherwise. Inconsistent in that code that looks and feels correct isn’t because of a lower case letter.

Not going to argue how ridiculous I find it to not have a reserved word specifically for the function of “exporting” / “exposing”. Even though I find it incredibly obscure, I don’t care about trying to change opinions on it.

I do care about wasting time with a simple json encode/decode wondering why I had pretty much identical code, but mine produced no results or threw any type of error.

Would suggest being a lot more specific, where the topic is brought up
https://tour.golang.org/basics/3

Or even stress it here and anywhere else it could throw off any new comers to the language.
https://tour.golang.org/moretypes/3


(Christoph Berger) #2

Hi @divx,

If your complaint is specifically about the Go Tour, I suggest clicking the “bug” icon in the top right corner of the Go Tour page and report the issue there.

This is not an official Go forum, and I doubt that the Go Tour authors are monitoring this forum, at least not constantly.

I agree that the Go Tour is far from clear about the role of letter case in exporting struct fields. However, the Go Tour is very shallow in general, and I recommend reading through Effective Go that explains all specific features of Go, and keeping the Language Specification at hand that should have an answer for any language question - although some say it is a bit difficult to read. (Both docs available via the Documentation page.)

And BTW, having no reserved keyword for marking fields as exported might feel a bit unfamiliar at first, but once you get used to it, it gets very easy to distinguish exported fields from non-exported ones.


(Mike) #3

Thank you for the reply. I didn’t see a “report” feature on the “Go Tour”. Double checked after disabling everything on chrome that might block anything.

I have no doubt I’ll get “used to it”, but it’s a completely different course change compared to several major other languages I’ve used. It’s part of the lexical DNA, how the language operates and it’s a topic barely glanced at in any documentation, including the links offered. There’s no contextual glue. It feels like that uneven step in front of the hall newcomers trip on with an improper sign.

https://golang.org/doc/effective_go.html
(#names)
“Names are as important in Go as in any other language. They even have semantic effect: the visibility of a name outside a package is determined by whether its first character is upper case. It’s therefore worth spending a little time talking about naming conventions in Go programs.”

(#Exported_identifiers)
"An identifier may be exported to permit access to it from another package. An identifier is exported if both:

the first character of the identifier’s name is a Unicode upper case letter (Unicode class “Lu”); and
the identifier is declared in the package block or it is a field name or method name.
All other identifiers are not exported."

A commented example is a lot more crystal clear. Could offer more examples. May seem silly, but the difference in is staggering.

// Foobar.foo & Foobar.bar and function Add have "private scope"; ONLY the package can directly access it
type Foobar struct {
    foo int
    bar int
}

func add(x int, y int) int {
	return x + y
}

// Foobar.Foo & Foobar.Bar and function Add have "public scope"; ANYTHING can directly access it
type Foobar struct {
    Foo int
    Bar int
}

func Add(x int, y int) int {
	return x + y
}

Would be much better a reserved word, especially since it’s even stressed under “Minimize the exported interface” (https://blog.golang.org/organizing-go-code)

“The larger the interface you provide, the more you must support. Users will quickly come to depend on every type, function, variable, and constant you export, creating an implicit contract that you must honor in perpetuity or risk breaking your users’ programs.”

For me, it’s a big negative mark on the language. Is there even a way of quickly identifying exports (reliably without regex) via a text search or some inquiry using “go list”? Would be if there’s a reserved word for it.

Can live with it, but fix the signage or the step. Given it’d have an arduous ripple effect throughout the community, I’d lean towards the signage. Make it a more prominent topic throughout most of the documentation. Constantly pounding the idea that “if you lead anything with a Capital letter, it’s open to the world, it is public”.


(nathankerr) #4

Is there even a way of quickly identifying exports (reliably without regex) via a text search or some inquiry using “go list”?

go doc will do this.

If that doesn’t quite do what you want, use https://godoc.org/golang.org/x/tools/go/loader to build exactly what you want. With a little work you can get down to https://godoc.org/go/types#Object, which has IsExported() to tell you what you want.


(Christoph Berger) #5

Not sure why the button is missing at your end - it should be in the top right corner (see the screenshot).

The button is a simple link to a new issue on GitHub.

And about the pros and cons of using letter case for visibility: I find it absolutely easy to work with this concept, but I can only speak for myself.


(Mike) #6

“go doc” would work. Not as simple as a quick text search for a reserved word, but could write an IDE plugin to utilize it for the same general purpose.

@christophberger Found the button, thanks. Really not sure how I missed it. Will add my 2 cents on the respective pages later on.


(system) #7

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.