Skip to content

Instantly share code, notes, and snippets.

@lxdlam
Created April 25, 2025 08:17
Show Gist options
  • Save lxdlam/0104551ca5241cfd7894f8e82d0d3fc5 to your computer and use it in GitHub Desktop.
Save lxdlam/0104551ca5241cfd7894f8e82d0d3fc5 to your computer and use it in GitHub Desktop.
package main
import "fmt"
type readonly struct{}
type readwrite struct{}
type dbAcl interface{ readonly | readwrite }
type Driver[acl dbAcl] struct {
value int
}
func (d *Driver[_]) Read() int {
return d.value
}
func (d *Driver[readwrite]) Write(i int) {
d.value = i
}
func NewReadOnly() Driver[readonly] {
return Driver[readonly]{value: 15}
}
func NewReadWrite() Driver[readwrite] {
return Driver[readwrite]{value: 15}
}
func main() {
drw := NewReadWrite()
fmt.Println(drw.Read())
drw.Write(10)
fmt.Println(drw.Read())
dro := NewReadOnly()
fmt.Println(dro.Read())
dro.Write(10)
fmt.Println(dro.Read())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment