Documentation ¶
Overview ¶
Package pragma provides types that can be embedded into a struct to statically enforce or prevent certain language properties. The key observation and some code (shr) is borrowed from https://github.com/protocolbuffers/protobuf-go/blob/v1.25.0/internal/pragma/pragma.go
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CopyChecker ¶
type CopyChecker uintptr
CopyChecker holds back pointer to itself to detect object copying. Deprecated. use DoNotCopy instead, check by go vet. methods Copied or Check return not copied if none of methods Copied or Check have bee called before
Example ¶
package main import ( "fmt" "github.com/searKing/golang/go/pragma" ) type CopyChecker struct { pragma.CopyChecker } func main() { var a, b CopyChecker fmt.Printf("a copied : %t\n", a.Copied()) fmt.Printf("a copied : %t\n", a.Copied()) b = a fmt.Printf("a copied : %t\n", a.Copied()) fmt.Printf("b copied : %t\n", b.Copied()) }
Output: a copied : false a copied : false a copied : false b copied : true
func (*CopyChecker) Copied ¶
func (c *CopyChecker) Copied() bool
Copied returns true if this object is copied
type DoNotCompare ¶
type DoNotCompare [0]func()
DoNotCompare can be embedded in a struct to prevent comparability.
Example ¶
package main import ( "fmt" ) func main() { // var a, b DoNotCompare // a == b // compile error will print if uncomment codes above fmt.Println("Invalid operation: a == b (operator == is not defined on DoNotCompare)") }
Output: Invalid operation: a == b (operator == is not defined on DoNotCompare)
type DoNotCopy ¶
DoNotCopy can be embedded in a struct to help prevent shallow copies. This does not rely on a Go language feature, but rather a special case within the vet checker.
See https://golang.org/issues/8005.
Example ¶
package main import ( "fmt" ) func main() { //var a DoNotCopy //b := a //_ = b // go vet error will print if uncomment codes above fmt.Println("Assignment copies lock value to '_': type 'DoNotCopy' contains 'sync.Mutex' which is 'sync.Locker'") }
Output: Assignment copies lock value to '_': type 'DoNotCopy' contains 'sync.Mutex' which is 'sync.Locker'
type DoNotImplement ¶
type DoNotImplement interface{ ProtoInternal(doNotImplement) }
Example ¶
package main import ( "fmt" "github.com/searKing/golang/go/pragma" ) type DoNotImplementStruct struct { } func (*DoNotImplementStruct) ProtoInternal(pragma.DoNotImplement) { } func (*DoNotImplementStruct) String() string { return "whoops" } func main() { var a DoNotImplementStruct _ = a // You can never implement this interface, with pragma.DoNotImplement embed //var b DoNotImplement = &a // go vet error will print if uncomment codes above fmt.Println("cannot use &a (type *DoNotImplementStruct) as type DoNotImplement in assignment:") }
Output: cannot use &a (type *DoNotImplementStruct) as type DoNotImplement in assignment:
type NoUnkeyedLiterals ¶
type NoUnkeyedLiterals struct{}
NoUnkeyedLiterals can be embedded in a struct to prevent unkeyed literals.
Example ¶
package main import ( "fmt" ) func main() { //var a = NoUnkeyedLiterals{"Name"} //_ = a // compile error will print if uncomment codes above fmt.Println(`cannot convert "Name" (untyped string constant) to pragma.NoUnkeyedLiterals`) }
Output: cannot convert "Name" (untyped string constant) to pragma.NoUnkeyedLiterals