Skip to content

proposal: Go 2: introduce "grant" keyword to set access to variables and functions #31632

Closed
@StephanVerbeeck

Description

@StephanVerbeeck

About: read-only, private, public, protected, const

Proposal: introduce Grant keyword

This would fixes all these issues #27975 #20443 #22876 #22048 without introducing compatibility issues of any kind. These issues are currently all open and requiring decision that alter the behavior of existing code while my proposal solves all these issues without any impact on existing code.

Introduction to "grant".

the keyword "grant" comes from the SQL language where it is used to assign privilege to use a particular item in a particular way to a particular user or group of users.

In established programming languages (C++, C#, JAVA) this functionality is accomplished by using keywords like:

  • private
  • public
  • const
  • protected
  • friend

However these keywords introduce complexity that we do NOT WANT in GO.

So my proposal is to introduce an optional keyword that does the same but BETTER, FASTER, STRICTER and more ACCURATE and more READABLE.

The new grant keyword is only used during compile time and does not generate any run-time code.

In SQL we have privileges like INSERT, UPDATE, DELETE, CREATE, DROP, ...
In GO we have privileges GET, SET, USE

In SQL we have users and groups of users (because SQL database requires login-to-database with user credentials) so grants are made to a user.
In GO we do not know who the programmer or user is so the grant is made BETWEEN items (where an item is a type or method or variable or package)

Because "grant" is optional the default is that all privileges are granted between all items (not changing the local/public flag set by a name starting with lower-case or upper-case rune).

The effect of the grant keyword is restricted to the package in which it occurs.
Multiple grants (e.g. in different sources about the same item) are cumulative.
There is no "revoke" (like in SQL) that could undo a grant. To revoke a right you simply change the source code line that does the Grant.

Examples

situation 1: I have a structure with a counter that everybody may read but only a single function should be allowed to increment or decrement.

grant set (
	Data.counter Increment Decrement // write access to this variable limited to these 2 methods
)

grant get (
	Data.counter // read access to everybody (because nobody specified so that means everybody)
)

grant (
	Data // all actions allowed by everybody (this statement is not needed because it is the default)
)
	
type Data struct {
	a,b,c string
	counter int
}

func (data *Data) Increment() {
	data.counter++
}

func (data *Data) Decrement() {
	data.counter--
}

situation 2: I want to limit all access to function Aaa() to only function Bbbb() (undoing the default grant all to all)

grant use (
	Aaaa Bbbb // Only Bbbb() may call Aaaa()
)

situation 3: I have a variable that needs initialization but only the function that initializes it may write to it (everywhere else all members of the struct are immutable)

grant set a,b,c MyInitFunction

var (
	a,b,c int
)

func MyInitFunction(){
	a = 1 // this compiles
	b = 2
	c = 3
}

func MyOtherFunction(){
	a = 1 // this does not compile, generates error "set 'a' not granted for 'MyOtherFunction'"
	b = 2
	c = 3
}

situation 4: I have dot-imported a package but want to restrict the use of this "type wildcard" to a few functions

package main

import (
	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
)

grant (
	declarative RunProgramDialog
)

func RunProgramDialog(program *Program) (int, error) {

	var (
		dataBinder *walk.DataBinder
		dlg        *walk.Dialog
		acceptPB   *walk.PushButton
		cancelPB   *walk.PushButton
		fileName   *walk.ComboBox
	)
	return Dialog{
		... // to much code lines to include in this example
	}.Run(mainWindow)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions