Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: spec: init-only package level variables #20598

Open
iand opened this issue Jun 7, 2017 · 4 comments

Comments

@iand
Copy link
Contributor

commented Jun 7, 2017

Mutable state held in package-level variables can be problematic for a few reasons, e.g. concurrency and repeated imports of a single package.

It would be useful to be able to declare a form of "assign-once" variable that can only have its value set via an initializer in the declaration or in a subsequent init function. Once all init functions have completed the value of the variable cannot be changed.

Some other languages use a special keyword to indicate this type of variable (e.g. pony uses let for assign-once variables and var for others)

@ianlancetaylor ianlancetaylor changed the title Init-only package level variables proposal: init-only package level variables Jun 7, 2017

@gopherbot gopherbot added this to the Proposal milestone Jun 7, 2017

@gopherbot gopherbot added the Proposal label Jun 7, 2017

@ianlancetaylor ianlancetaylor added Go2 Proposal and removed Proposal labels Jun 7, 2017

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jun 7, 2017

This is a language change so marking as a proposal for Go 2.

@griesemer

This comment has been minimized.

Copy link
Contributor

commented Jun 7, 2017

@iand This is somewhat vague a proposal. Can you elaborate a bit? For instance, how "deep" is the "freezing" of such assign-once variables? If I have a pointer or slice in an assign-once variable, can I still change the contents of the value pointed to or the elements of the slice?

@josharian

This comment has been minimized.

Copy link
Contributor

commented Jun 7, 2017

I can imagine how to implement this for composite liberals, but it is harder for init functions—many creative ways to get bits of the structure to escape, particularly if it is a compile time check.

One nice thing about this for maps is that it would let the compiler substitute data structures optimized for lookups, which could provide very substantial speed-ups.

@andlabs

This comment has been minimized.

Copy link
Contributor

commented Jun 7, 2017

"repeated imports of a single package" — aren't packages only imported and initialized once in an output binary, thanks to the guarantee of no import cycles? Do c-archives/c-shareds/plugins each have their own separate copies of packages?

@rsc rsc changed the title proposal: init-only package level variables proposal: spec: init-only package level variables Jun 16, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.