aboutsummaryrefslogtreecommitdiff
path: root/content/constants
diff options
context:
space:
mode:
Diffstat (limited to 'content/constants')
-rw-r--r--content/constants/bool.go18
-rw-r--r--content/constants/complex1.go18
-rw-r--r--content/constants/complex2.go13
-rw-r--r--content/constants/complex3.go15
-rw-r--r--content/constants/default1.go11
-rw-r--r--content/constants/default2.go14
-rw-r--r--content/constants/default3.go15
-rw-r--r--content/constants/exercise1.go9
-rw-r--r--content/constants/exercise2.go9
-rw-r--r--content/constants/exercise3.go12
-rw-r--r--content/constants/exercise4.go12
-rw-r--r--content/constants/exercise5.go9
-rw-r--r--content/constants/exercise6.go12
-rw-r--r--content/constants/float1.go18
-rw-r--r--content/constants/float2.go17
-rw-r--r--content/constants/float3.go12
-rw-r--r--content/constants/float4.go12
-rw-r--r--content/constants/float5.go15
-rw-r--r--content/constants/int1.go18
-rw-r--r--content/constants/int2.go10
-rw-r--r--content/constants/int3.go10
-rw-r--r--content/constants/int4.go11
-rw-r--r--content/constants/numbers1.go19
-rw-r--r--content/constants/numbers2.go12
-rw-r--r--content/constants/string1.go15
-rw-r--r--content/constants/string2.go16
-rw-r--r--content/constants/string3.go17
-rw-r--r--content/constants/string4.go16
-rw-r--r--content/constants/syntax.go14
29 files changed, 399 insertions, 0 deletions
diff --git a/content/constants/bool.go b/content/constants/bool.go
new file mode 100644
index 0000000..7578d4a
--- /dev/null
+++ b/content/constants/bool.go
@@ -0,0 +1,18 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ // START OMIT
+ type MyBool bool
+ const True = true
+ const TypedTrue bool = true
+ var mb MyBool
+ mb = true // OK
+ mb = True // OK
+ mb = TypedTrue // Bad
+ fmt.Println(mb)
+ // STOP OMIT
+}
diff --git a/content/constants/complex1.go b/content/constants/complex1.go
new file mode 100644
index 0000000..ea45921
--- /dev/null
+++ b/content/constants/complex1.go
@@ -0,0 +1,18 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ // START OMIT
+ type MyComplex128 complex128
+ const I = (0.0 + 1.0i)
+ const TypedI complex128 = (0.0 + 1.0i)
+ var mc MyComplex128
+ mc = (0.0 + 1.0i) // OK
+ mc = I // OK
+ mc = TypedI // Bad
+ fmt.Println(mc)
+ // STOP OMIT
+}
diff --git a/content/constants/complex2.go b/content/constants/complex2.go
new file mode 100644
index 0000000..5dd4f73
--- /dev/null
+++ b/content/constants/complex2.go
@@ -0,0 +1,13 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ const Two = 2.0 + 0i
+ // START OMIT
+ s := Two
+ fmt.Printf("%T: %v\n", s, s)
+ // STOP OMIT
+}
diff --git a/content/constants/complex3.go b/content/constants/complex3.go
new file mode 100644
index 0000000..95e9e41
--- /dev/null
+++ b/content/constants/complex3.go
@@ -0,0 +1,15 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ const Two = 2.0 + 0i
+ // START OMIT
+ var f float64
+ var g float64 = Two
+ f = Two
+ fmt.Println(f, "and", g)
+ // STOP OMIT
+}
diff --git a/content/constants/default1.go b/content/constants/default1.go
new file mode 100644
index 0000000..914e4b7
--- /dev/null
+++ b/content/constants/default1.go
@@ -0,0 +1,11 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ // START OMIT
+ fmt.Printf("Hello, 世界")
+ // STOP OMIT
+}
diff --git a/content/constants/default2.go b/content/constants/default2.go
new file mode 100644
index 0000000..2183cf0
--- /dev/null
+++ b/content/constants/default2.go
@@ -0,0 +1,14 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+const hello = "Hello, 世界"
+
+func main() {
+ // START OMIT
+ fmt.Printf("%T: %v\n", "Hello, 世界", "Hello, 世界")
+ fmt.Printf("%T: %v\n", hello, hello)
+ // STOP OMIT
+}
diff --git a/content/constants/default3.go b/content/constants/default3.go
new file mode 100644
index 0000000..246ae45
--- /dev/null
+++ b/content/constants/default3.go
@@ -0,0 +1,15 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+type MyString string
+
+const myStringHello MyString = "Hello, 世界"
+
+func main() {
+ // START OMIT
+ fmt.Printf("%T: %v\n", myStringHello, myStringHello)
+ // STOP OMIT
+}
diff --git a/content/constants/exercise1.go b/content/constants/exercise1.go
new file mode 100644
index 0000000..881d1d0
--- /dev/null
+++ b/content/constants/exercise1.go
@@ -0,0 +1,9 @@
+// +build OMIT
+
+package main
+
+func main() {
+ // START OMIT
+ const MaxUint uint = -1 // Error: negative value
+ // STOP OMIT
+}
diff --git a/content/constants/exercise2.go b/content/constants/exercise2.go
new file mode 100644
index 0000000..db7d8c1
--- /dev/null
+++ b/content/constants/exercise2.go
@@ -0,0 +1,9 @@
+// +build OMIT
+
+package main
+
+func main() {
+ // START OMIT
+ const MaxUint uint = uint(-1) // Error: negative value
+ // STOP OMIT
+}
diff --git a/content/constants/exercise3.go b/content/constants/exercise3.go
new file mode 100644
index 0000000..dbe7595
--- /dev/null
+++ b/content/constants/exercise3.go
@@ -0,0 +1,12 @@
+// +build OMIT
+
+package main
+
+func main() {
+ // START OMIT
+ var u uint
+ var v = -1
+ u = uint(v)
+ // STOP OMIT
+ _ = u
+}
diff --git a/content/constants/exercise4.go b/content/constants/exercise4.go
new file mode 100644
index 0000000..3acc664
--- /dev/null
+++ b/content/constants/exercise4.go
@@ -0,0 +1,12 @@
+// +build OMIT
+
+package main
+
+func main() {
+ // START OMIT
+ var u uint
+ const v = -1
+ u = uint(v) // Error: negative value
+ // STOP OMIT
+ _ = u
+}
diff --git a/content/constants/exercise5.go b/content/constants/exercise5.go
new file mode 100644
index 0000000..2fca330
--- /dev/null
+++ b/content/constants/exercise5.go
@@ -0,0 +1,9 @@
+// +build OMIT
+
+package main
+
+func main() {
+ // START OMIT
+ const MaxUint uint = ^0 // Error: overflow
+ // STOP OMIT
+}
diff --git a/content/constants/exercise6.go b/content/constants/exercise6.go
new file mode 100644
index 0000000..ce6478c
--- /dev/null
+++ b/content/constants/exercise6.go
@@ -0,0 +1,12 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ // START OMIT
+ const MaxUint = ^uint(0)
+ fmt.Printf("%x\n", MaxUint)
+ // STOP OMIT
+}
diff --git a/content/constants/float1.go b/content/constants/float1.go
new file mode 100644
index 0000000..96b3bb9
--- /dev/null
+++ b/content/constants/float1.go
@@ -0,0 +1,18 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ // START OMIT
+ type MyFloat64 float64
+ const Zero = 0.0
+ const TypedZero float64 = 0.0
+ var mf MyFloat64
+ mf = 0.0 // OK
+ mf = Zero // OK
+ mf = TypedZero // Bad
+ fmt.Println(mf)
+ // STOP OMIT
+}
diff --git a/content/constants/float2.go b/content/constants/float2.go
new file mode 100644
index 0000000..f43feb6
--- /dev/null
+++ b/content/constants/float2.go
@@ -0,0 +1,17 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ const Zero = 0.0
+ const TypedZero float64 = 0.0
+ // START OMIT
+ var f32 float32
+ f32 = 0.0
+ f32 = Zero // OK: Zero is untyped
+ f32 = TypedZero // Bad: TypedZero is float64 not float32.
+ fmt.Println(f32)
+ // STOP OMIT
+}
diff --git a/content/constants/float3.go b/content/constants/float3.go
new file mode 100644
index 0000000..d862acc
--- /dev/null
+++ b/content/constants/float3.go
@@ -0,0 +1,12 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ const Huge = 1e1000
+ // START OMIT
+ fmt.Println(Huge)
+ // STOP OMIT
+}
diff --git a/content/constants/float4.go b/content/constants/float4.go
new file mode 100644
index 0000000..206d215
--- /dev/null
+++ b/content/constants/float4.go
@@ -0,0 +1,12 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ const Huge = 1e1000
+ // START OMIT
+ fmt.Println(Huge / 1e999)
+ // STOP OMIT
+}
diff --git a/content/constants/float5.go b/content/constants/float5.go
new file mode 100644
index 0000000..2d62872
--- /dev/null
+++ b/content/constants/float5.go
@@ -0,0 +1,15 @@
+// +build OMIT
+
+package main
+
+import (
+ "fmt"
+ "math"
+)
+
+func main() {
+ // START OMIT
+ pi := math.Pi
+ fmt.Println(pi)
+ // STOP OMIT
+}
diff --git a/content/constants/int1.go b/content/constants/int1.go
new file mode 100644
index 0000000..913ad78
--- /dev/null
+++ b/content/constants/int1.go
@@ -0,0 +1,18 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ // START OMIT
+ type MyInt int
+ const Three = 3
+ const TypedThree int = 3
+ var mi MyInt
+ mi = 3 // OK
+ mi = Three // OK
+ mi = TypedThree // Bad
+ fmt.Println(mi)
+ // STOP OMIT
+}
diff --git a/content/constants/int2.go b/content/constants/int2.go
new file mode 100644
index 0000000..0e974ef
--- /dev/null
+++ b/content/constants/int2.go
@@ -0,0 +1,10 @@
+// +build OMIT
+
+package main
+
+func main() {
+ // START OMIT
+ var i8 int8 = 128 // Error: too large.
+ // STOP OMIT
+ _ = i8
+}
diff --git a/content/constants/int3.go b/content/constants/int3.go
new file mode 100644
index 0000000..c2cec6e
--- /dev/null
+++ b/content/constants/int3.go
@@ -0,0 +1,10 @@
+// +build OMIT
+
+package main
+
+func main() {
+ // START OMIT
+ var u8 uint8 = -1 // Error: negative value.
+ // STOP OMIT
+ _ = u8
+}
diff --git a/content/constants/int4.go b/content/constants/int4.go
new file mode 100644
index 0000000..caff695
--- /dev/null
+++ b/content/constants/int4.go
@@ -0,0 +1,11 @@
+// +build OMIT
+
+package main
+
+func main() {
+ // START OMIT
+ type Char byte
+ var c Char = '世' // Error: '世' has value 0x4e16, too large.
+ // STOP OMIT
+ _ = c
+}
diff --git a/content/constants/numbers1.go b/content/constants/numbers1.go
new file mode 100644
index 0000000..ea35a51
--- /dev/null
+++ b/content/constants/numbers1.go
@@ -0,0 +1,19 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ // START OMIT
+ var f float32 = 1
+ var i int = 1.000
+ var u uint32 = 1e3 - 99.0*10.0 - 9
+ var c float64 = '\x01'
+ var p uintptr = '\u0001'
+ var r complex64 = 'b' - 'a'
+ var b byte = 1.0 + 3i - 3.0i
+
+ fmt.Println(f, i, u, c, p, r, b)
+ // STOP OMIT
+}
diff --git a/content/constants/numbers2.go b/content/constants/numbers2.go
new file mode 100644
index 0000000..33b7de0
--- /dev/null
+++ b/content/constants/numbers2.go
@@ -0,0 +1,12 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ // START OMIT
+ var f = 'a' * 1.5
+ fmt.Println(f)
+ // STOP OMIT
+}
diff --git a/content/constants/string1.go b/content/constants/string1.go
new file mode 100644
index 0000000..3b83851
--- /dev/null
+++ b/content/constants/string1.go
@@ -0,0 +1,15 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+const typedHello string = "Hello, 世界"
+
+func main() {
+ // START OMIT
+ var s string
+ s = typedHello
+ fmt.Println(s)
+ // STOP OMIT
+}
diff --git a/content/constants/string2.go b/content/constants/string2.go
new file mode 100644
index 0000000..362b484
--- /dev/null
+++ b/content/constants/string2.go
@@ -0,0 +1,16 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+const typedHello string = "Hello, 世界"
+
+func main() {
+ // START OMIT
+ type MyString string
+ var m MyString
+ m = typedHello // Type error
+ fmt.Println(m)
+ // STOP OMIT
+}
diff --git a/content/constants/string3.go b/content/constants/string3.go
new file mode 100644
index 0000000..72de35f
--- /dev/null
+++ b/content/constants/string3.go
@@ -0,0 +1,17 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+const typedHello string = "Hello, 世界"
+
+func main() {
+ type MyString string
+ var m MyString
+ // START OMIT
+ const myStringHello MyString = "Hello, 世界"
+ m = myStringHello // OK
+ fmt.Println(m)
+ // STOP OMIT
+}
diff --git a/content/constants/string4.go b/content/constants/string4.go
new file mode 100644
index 0000000..4c35f29
--- /dev/null
+++ b/content/constants/string4.go
@@ -0,0 +1,16 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+const typedHello string = "Hello, 世界"
+
+func main() {
+ type MyString string
+ var m MyString
+ // START OMIT
+ m = MyString(typedHello)
+ fmt.Println(m)
+ // STOP OMIT
+}
diff --git a/content/constants/syntax.go b/content/constants/syntax.go
new file mode 100644
index 0000000..16e7e64
--- /dev/null
+++ b/content/constants/syntax.go
@@ -0,0 +1,14 @@
+// +build OMIT
+
+package main
+
+import "fmt"
+
+func main() {
+ // START OMIT
+ fmt.Printf("%T %v\n", 0, 0)
+ fmt.Printf("%T %v\n", 0.0, 0.0)
+ fmt.Printf("%T %v\n", 'x', 'x')
+ fmt.Printf("%T %v\n", 0i, 0i)
+ // STOP OMIT
+}