github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/go/constant/example_test.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package constant_test 6 7 import ( 8 "fmt" 9 "go/constant" 10 "go/token" 11 "sort" 12 ) 13 14 func Example_complexNumbers() { 15 // Create the complex number 2.3 + 5i. 16 ar := constant.MakeFloat64(2.3) 17 ai := constant.MakeImag(constant.MakeInt64(5)) 18 a := constant.BinaryOp(ar, token.ADD, ai) 19 20 // Compute (2.3 + 5i) * 11. 21 b := constant.MakeUint64(11) 22 c := constant.BinaryOp(a, token.MUL, b) 23 24 // Convert c into a complex128. 25 Ar, exact := constant.Float64Val(constant.Real(c)) 26 if !exact { 27 fmt.Printf("Could not represent real part %s exactly as float64\n", constant.Real(c)) 28 } 29 Ai, exact := constant.Float64Val(constant.Imag(c)) 30 if !exact { 31 fmt.Printf("Could not represent imaginary part %s as exactly as float64\n", constant.Imag(c)) 32 } 33 C := complex(Ar, Ai) 34 35 fmt.Println("literal", 25.3+55i) 36 fmt.Println("go/constant", c) 37 fmt.Println("complex128", C) 38 39 // Output: 40 // 41 // Could not represent real part 25.3 exactly as float64 42 // literal (25.3+55i) 43 // go/constant (25.3 + 55i) 44 // complex128 (25.299999999999997+55i) 45 } 46 47 func ExampleBinaryOp() { 48 // 11 / 0.5 49 a := constant.MakeUint64(11) 50 b := constant.MakeFloat64(0.5) 51 c := constant.BinaryOp(a, token.QUO, b) 52 fmt.Println(c) 53 54 // Output: 22 55 } 56 57 func ExampleUnaryOp() { 58 vs := []constant.Value{ 59 constant.MakeBool(true), 60 constant.MakeFloat64(2.7), 61 constant.MakeUint64(42), 62 } 63 64 for i, v := range vs { 65 switch v.Kind() { 66 case constant.Bool: 67 vs[i] = constant.UnaryOp(token.NOT, v, 0) 68 69 case constant.Float: 70 vs[i] = constant.UnaryOp(token.SUB, v, 0) 71 72 case constant.Int: 73 // Use 16-bit precision. 74 // This would be equivalent to ^uint16(v). 75 vs[i] = constant.UnaryOp(token.XOR, v, 16) 76 } 77 } 78 79 for _, v := range vs { 80 fmt.Println(v) 81 } 82 83 // Output: 84 // 85 // false 86 // -2.7 87 // 65493 88 } 89 90 func ExampleCompare() { 91 vs := []constant.Value{ 92 constant.MakeString("Z"), 93 constant.MakeString("bacon"), 94 constant.MakeString("go"), 95 constant.MakeString("Frame"), 96 constant.MakeString("defer"), 97 constant.MakeFromLiteral(`"a"`, token.STRING, 0), 98 } 99 100 sort.Slice(vs, func(i, j int) bool { 101 // Equivalent to vs[i] <= vs[j]. 102 return constant.Compare(vs[i], token.LEQ, vs[j]) 103 }) 104 105 for _, v := range vs { 106 fmt.Println(constant.StringVal(v)) 107 } 108 109 // Output: 110 // 111 // Frame 112 // Z 113 // a 114 // bacon 115 // defer 116 // go 117 } 118 119 func ExampleSign() { 120 zero := constant.MakeInt64(0) 121 one := constant.MakeInt64(1) 122 negOne := constant.MakeInt64(-1) 123 124 mkComplex := func(a, b constant.Value) constant.Value { 125 b = constant.MakeImag(b) 126 return constant.BinaryOp(a, token.ADD, b) 127 } 128 129 vs := []constant.Value{ 130 negOne, 131 mkComplex(zero, negOne), 132 mkComplex(one, negOne), 133 mkComplex(negOne, one), 134 mkComplex(negOne, negOne), 135 zero, 136 mkComplex(zero, zero), 137 one, 138 mkComplex(zero, one), 139 mkComplex(one, one), 140 } 141 142 for _, v := range vs { 143 fmt.Printf("% d %s\n", constant.Sign(v), v) 144 } 145 146 // Output: 147 // 148 // -1 -1 149 // -1 (0 + -1i) 150 // -1 (1 + -1i) 151 // -1 (-1 + 1i) 152 // -1 (-1 + -1i) 153 // 0 0 154 // 0 (0 + 0i) 155 // 1 1 156 // 1 (0 + 1i) 157 // 1 (1 + 1i) 158 }