From 2e338fa69f269e14d5dc1923c033c9a16b4bce5c Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Fri, 9 Dec 2011 08:31:57 -0800 Subject: [PATCH] doc/go1: the rest of the language changes R=rsc CC=golang-dev https://golang.org/cl/5478047 --- doc/go1.html | 152 ++++++++++++++++++++++++++++++++++++++++++++++- doc/go1.tmpl | 115 ++++++++++++++++++++++++++++++++++- doc/progs/go1.go | 52 +++++++++++++++- 3 files changed, 316 insertions(+), 3 deletions(-) diff --git a/doc/go1.html b/doc/go1.html index 4ac6924912..1472cb3f54 100644 --- a/doc/go1.html +++ b/doc/go1.html @@ -90,6 +90,51 @@ now reject such code.

Composite literals

+

+In Go 1, a composite literal of array, slice, or map type can elide the +type specification for the elements' initializers if they are of pointer type. +All four of the initializations in this example are legal; the last one was illegal before Go 1. +

+ +
    type Date struct {
+        month string
+        day   int
+    }
+    // Struct values, fully qualified; always legal.
+    holiday1 := []Date{
+        Date{"Feb", 14},
+        Date{"Nov", 11},
+        Date{"Dec", 25},
+    }
+    // Struct values, type name elided; always legal.
+    holiday2 := []Date{
+        {"Feb", 14},
+        {"Nov", 11},
+        {"Dec", 25},
+    }
+    // Pointers, fully qualified, always legal.
+    holiday3 := []*Date{
+        &Date{"Feb", 14},
+        &Date{"Nov", 11},
+        &Date{"Dec", 25},
+    }
+    // Pointers, type name elided; legal in Go 1.
+    holiday4 := []*Date{
+        {"Feb", 14},
+        {"Nov", 11},
+        {"Dec", 25},
+    }
+
+ +

+Updating: +This change has no effect on existing code, but the command +gofmt -s applied to existing source +will, among other things, elide explicit element types wherever permitted. +

+ +

Goroutines during init

@@ -119,6 +164,62 @@ There was no such code in the standard repository.

The rune type

+

+Go 1 introduces a new basic type, rune, to be used to represent +individual Unicode code points. +It is an alias for int32, analogous to byte +as an alias for uint8. +

+ +

+Character literals such as 'a', '語', and '\u0345' +now have default type rune, +analogous to 1.0 having default type float64. +A variable initialized to a character constant will therefore +have type rune unless otherwise specified. +

+ +

+Libraries have been updated to use rune rather than int +when appropriate. For instance, the functions unicode.ToLower and +relatives now take and return a rune. +

+ +
    delta := 'δ' // delta has type rune.
+    var DELTA rune
+    DELTA = unicode.ToUpper(delta)
+    epsilon := unicode.ToLower(DELTA + 1)
+    if epsilon != 'δ'+1 {
+        log.Fatal("inconsistent casing for Greek")
+    }
+
+ +

+Updating: +Most source code will be unaffected by this because the type inference from +:= initializers introduces the new type silently, and it propagates +from there. +Some code may get type errors that a trivial conversion will resolve. +

+ +

The error type

+ +

+Go 1 introduces a new built-in type, error, which has the following definition: +

+ +
+    type error interface {
+        Error() string
+    }
+
+ +

+Since the consequences of this type are all in the package library, +it is discussed below. +

+

Deleting from maps

@@ -126,7 +227,7 @@ The original syntax for deleting an element in a map was:

-    m[x] = ignored, false
+    m[k] = ignored, false
 

@@ -210,6 +311,7 @@ These examples illustrate the behavior. sc[0], sc[0] = 1, 2 // sets sc[0] = 1, then sc[0] = 2 (so sc[0] = 2 at end) +

Updating: This is one change where tools cannot help, but breakage is unlikely. No code in the standard repository was broken by this change, and code @@ -252,6 +354,54 @@ The few cases that arose in the standard repository were mostly bugs.

Copying structs with unexported fields

+

+Go 1 relaxes the rules about accessing structs with unexported (lower-case) fields, +permitting a client package to assign (and therefore copy) such a struct. +Of course, the client package still cannot access such fields individually. +

+ +

+As an example, if package p includes the definitions, +

+ +
+    type Struct struct {
+        Public int
+        secret int
+    }
+    func NewStruct(a int) Struct {  // Note: not a pointer.
+        return Struct{a, f(a)}
+    }
+    func (s Struct) String() string {
+        return fmt.Sprintf("{%d (secret %d)}", s.Public, s.secret)
+    }
+
+ +

+a package that imports p can assign and copy values of type +p.Struct at will. +Behind the scenes the unexported fields will be assigned and copied just +as if they were exported, +but the client code will never be aware of them. The code +

+ +
+    import "p"
+
+    myStruct := p.NewStruct(23)
+    copyOfMyStruct := myStruct
+    fmt.Println(myStruct, copyOfMyStruct)
+
+ +

+will show that the secret field of the struct has been copied to the new value. +

+ +

+Updating: +This is a new feature, so existing code needs no changes. +

+

Equality of structs and arrays

diff --git a/doc/go1.tmpl b/doc/go1.tmpl index d317f3b0f0..04e72dfab2 100644 --- a/doc/go1.tmpl +++ b/doc/go1.tmpl @@ -81,6 +81,22 @@ now reject such code.

Composite literals

+

+In Go 1, a composite literal of array, slice, or map type can elide the +type specification for the elements' initializers if they are of pointer type. +All four of the initializations in this example are legal; the last one was illegal before Go 1. +

+ +{{code "progs/go1.go" `/type Date struct/` `/STOP/`}} + +

+Updating: +This change has no effect on existing code, but the command +gofmt -s applied to existing source +will, among other things, elide explicit element types wherever permitted. +

+ +

Goroutines during init

@@ -102,6 +118,54 @@ There was no such code in the standard repository.

The rune type

+

+Go 1 introduces a new basic type, rune, to be used to represent +individual Unicode code points. +It is an alias for int32, analogous to byte +as an alias for uint8. +

+ +

+Character literals such as 'a', '語', and '\u0345' +now have default type rune, +analogous to 1.0 having default type float64. +A variable initialized to a character constant will therefore +have type rune unless otherwise specified. +

+ +

+Libraries have been updated to use rune rather than int +when appropriate. For instance, the functions unicode.ToLower and +relatives now take and return a rune. +

+ +{{code "progs/go1.go" `/STARTRUNE/` `/ENDRUNE/`}} + +

+Updating: +Most source code will be unaffected by this because the type inference from +:= initializers introduces the new type silently, and it propagates +from there. +Some code may get type errors that a trivial conversion will resolve. +

+ +

The error type

+ +

+Go 1 introduces a new built-in type, error, which has the following definition: +

+ +
+    type error interface {
+        Error() string
+    }
+
+ +

+Since the consequences of this type are all in the package library, +it is discussed below. +

+

Deleting from maps

@@ -109,7 +173,7 @@ The original syntax for deleting an element in a map was:

-    m[x] = ignored, false
+    m[k] = ignored, false
 

@@ -174,6 +238,7 @@ These examples illustrate the behavior. {{code "progs/go1.go" `/sa :=/` `/then sc.0. = 2/`}} +

Updating: This is one change where tools cannot help, but breakage is unlikely. No code in the standard repository was broken by this change, and code @@ -216,6 +281,54 @@ The few cases that arose in the standard repository were mostly bugs.

Copying structs with unexported fields

+

+Go 1 relaxes the rules about accessing structs with unexported (lower-case) fields, +permitting a client package to assign (and therefore copy) such a struct. +Of course, the client package still cannot access such fields individually. +

+ +

+As an example, if package p includes the definitions, +

+ +
+    type Struct struct {
+        Public int
+        secret int
+    }
+    func NewStruct(a int) Struct {  // Note: not a pointer.
+        return Struct{a, f(a)}
+    }
+    func (s Struct) String() string {
+        return fmt.Sprintf("{%d (secret %d)}", s.Public, s.secret)
+    }
+
+ +

+a package that imports p can assign and copy values of type +p.Struct at will. +Behind the scenes the unexported fields will be assigned and copied just +as if they were exported, +but the client code will never be aware of them. The code +

+ +
+    import "p"
+
+    myStruct := p.NewStruct(23)
+    copyOfMyStruct := myStruct
+    fmt.Println(myStruct, copyOfMyStruct)
+
+ +

+will show that the secret field of the struct has been copied to the new value. +

+ +

+Updating: +This is a new feature, so existing code needs no changes. +

+

Equality of structs and arrays

diff --git a/doc/progs/go1.go b/doc/progs/go1.go index f02ede7403..caceb0513c 100644 --- a/doc/progs/go1.go +++ b/doc/progs/go1.go @@ -6,7 +6,10 @@ package main -import "log" +import ( + "log" + "unicode" +) func main() { stringAppend() @@ -14,6 +17,8 @@ func main() { mapIteration() multipleAssignment() structEquality() + compositeLiterals() + runeType() } func mapDelete() { @@ -80,6 +85,51 @@ func structEquality() { // fmt.Printf("Christmas is a holiday: %t\n", holiday[Christmas]) } +func compositeLiterals() { + type Date struct { + month string + day int + } + // Struct values, fully qualified; always legal. + holiday1 := []Date{ + Date{"Feb", 14}, + Date{"Nov", 11}, + Date{"Dec", 25}, + } + // Struct values, type name elided; always legal. + holiday2 := []Date{ + {"Feb", 14}, + {"Nov", 11}, + {"Dec", 25}, + } + // Pointers, fully qualified, always legal. + holiday3 := []*Date{ + &Date{"Feb", 14}, + &Date{"Nov", 11}, + &Date{"Dec", 25}, + } + // Pointers, type name elided; legal in Go 1. + holiday4 := []*Date{ + {"Feb", 14}, + {"Nov", 11}, + {"Dec", 25}, + } + // STOP OMIT + _, _, _, _ = holiday1, holiday2, holiday3, holiday4 +} + +func runeType() { + // STARTRUNE OMIT + delta := 'δ' // delta has type rune. + var DELTA rune + DELTA = unicode.ToUpper(delta) + epsilon := unicode.ToLower(DELTA + 1) + if epsilon != 'δ'+1 { + log.Fatal("inconsistent casing for Greek") + } + // ENDRUNE OMIT +} + func f(string, int) { } -- 2.50.0