github.com/searKing/golang/go@v1.2.117/pragma/example_test.go (about)

     1  package pragma_test
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/searKing/golang/go/pragma"
     6  )
     7  
     8  type NoUnkeyedLiterals struct {
     9  	pragma.NoUnkeyedLiterals
    10  	Name string
    11  }
    12  
    13  func ExampleNoUnkeyedLiterals() {
    14  	//var a = NoUnkeyedLiterals{"Name"}
    15  	//_ = a
    16  	// compile error will print if uncomment codes above
    17  	fmt.Println(`cannot convert "Name" (untyped string constant) to pragma.NoUnkeyedLiterals`)
    18  	// Output: cannot convert "Name" (untyped string constant) to pragma.NoUnkeyedLiterals
    19  }
    20  
    21  type DoNotImplement interface {
    22  	pragma.DoNotImplement
    23  	String() string
    24  }
    25  
    26  type DoNotImplementStruct struct {
    27  }
    28  
    29  func (*DoNotImplementStruct) ProtoInternal(pragma.DoNotImplement) {
    30  
    31  }
    32  
    33  func (*DoNotImplementStruct) String() string {
    34  	return "whoops"
    35  }
    36  
    37  func ExampleDoNotImplement() {
    38  	var a DoNotImplementStruct
    39  	_ = a
    40  
    41  	// You can never implement this interface, with pragma.DoNotImplement embed
    42  	//var b DoNotImplement = &a
    43  	// go vet error will print if uncomment codes above
    44  	fmt.Println("cannot use &a (type *DoNotImplementStruct) as type DoNotImplement in assignment:")
    45  
    46  	// Output: cannot use &a (type *DoNotImplementStruct) as type DoNotImplement in assignment:
    47  }
    48  
    49  type DoNotCopy struct {
    50  	pragma.DoNotCopy
    51  }
    52  
    53  func ExampleDoNotCopy() {
    54  	//var a DoNotCopy
    55  	//b := a
    56  	//_ = b
    57  	// go vet error will print if uncomment codes above
    58  	fmt.Println("Assignment copies lock value to '_': type 'DoNotCopy' contains 'sync.Mutex' which is 'sync.Locker'")
    59  
    60  	// Output: Assignment copies lock value to '_': type 'DoNotCopy' contains 'sync.Mutex' which is 'sync.Locker'
    61  }
    62  
    63  type DoNotCompare struct {
    64  	age int
    65  
    66  	pragma.DoNotCompare
    67  }
    68  
    69  func ExampleDoNotCompare() {
    70  	// var a, b DoNotCompare
    71  	// a == b
    72  	// compile error will print if uncomment codes above
    73  	fmt.Println("Invalid operation: a == b (operator == is not defined on DoNotCompare)")
    74  	// Output: Invalid operation: a == b (operator == is not defined on DoNotCompare)
    75  }
    76  
    77  type CopyChecker struct {
    78  	pragma.CopyChecker
    79  }
    80  
    81  func ExampleCopyChecker() {
    82  	var a, b CopyChecker
    83  	fmt.Printf("a copied : %t\n", a.Copied())
    84  	fmt.Printf("a copied : %t\n", a.Copied())
    85  	b = a
    86  	fmt.Printf("a copied : %t\n", a.Copied())
    87  	fmt.Printf("b copied : %t\n", b.Copied())
    88  
    89  	// Output:
    90  	// a copied : false
    91  	// a copied : false
    92  	// a copied : false
    93  	// b copied : true
    94  }