Created
April 25, 2025 08:17
-
-
Save lxdlam/0104551ca5241cfd7894f8e82d0d3fc5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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