Last active
November 2, 2023 08:19
-
-
Save nikolaydubina/5140491012b0606462025dbc74960b3e 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
// https://go.dev/play/p/1nLXyCDe1_3 | |
// because UNIX! 🐣 | |
package main | |
import "fmt" | |
var ( | |
Read = Permission{0x1} | |
Write = Permission{0x2} | |
Execute = Permission{0x4} | |
) | |
type Permission struct{ v uint8 } | |
var ss = map[Permission]string{ | |
Permission{}: "---", | |
Read: "r--", | |
Write: "-w-", | |
Execute: "--e", | |
Read.Add(Write): "rw-", | |
Read.Add(Execute): "r-e", | |
Write.Add(Execute): "-we", | |
Read.Add(Write).Add(Execute): "rwe", | |
} | |
func (a Permission) String() string { return ss[a] } | |
func (a Permission) Add(b Permission) Permission { return Permission{a.v | b.v} } | |
func (a Permission) Contains(b Permission) bool { return b.v&(a.v&b.v) == b.v } | |
func main() { | |
fmt.Println(Read) | |
fmt.Println(Read.Contains(Read.Add(Write))) | |
fmt.Println(Read.Add(Write).Contains(Read)) | |
fmt.Println(Read.Add(Write).Contains(Execute)) | |
fmt.Println(Read.Add(Write).Add(Execute)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment