</p>
<pre><!--{{code "progs/error.go" `/func openFile/` `/STOP/`}}
--->f, err := os.Open("filename.ext")
+--> f, err := os.Open("filename.ext")
if err != nil {
log.Fatal(err)
}
</p>
<pre><!--{{code "progs/error.go" `/func printErr/` `/STOP/`}}
--->f, err := Sqrt(-1)
+--> f, err := Sqrt(-1)
if err != nil {
fmt.Println(err)
}</pre>
</p>
<pre><!--{{code "progs/error.go" `/fmtError/` `/STOP/`}}
--->if f < 0 {
+--> if f < 0 {
return 0, fmt.Errorf("math: square root of negative number %g", f)
}</pre>
</p>
<pre><!--{{code "progs/error.go" `/func decodeError/` `/STOP/`}}
--->if err := dec.Decode(&val); err != nil {
+--> if err := dec.Decode(&val); err != nil {
if serr, ok := err.(*json.SyntaxError); ok {
line, col := findLine(f, serr.Offset)
return fmt.Errorf("%s:%d:%d: %v", f.Name(), line, col, err)
</p>
<pre><!--{{code "progs/error.go" `/func netError/` `/STOP/`}}
--->if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
+--> if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
time.Sleep(1e9)
continue
}
</p>
<pre><!--{{code "progs/go1.go" `/greeting := ..byte/` `/append.*hello/`}}
--->greeting := []byte{}
+--> greeting := []byte{}
greeting = append(greeting, []byte("hello ")...)</pre>
<p>
</p>
<pre><!--{{code "progs/go1.go" `/append.*world/`}}
--->greeting = append(greeting, "world"...)</pre>
+--> greeting = append(greeting, "world"...)</pre>
<p>
<em>Updating</em>:
</p>
<pre><!--{{code "progs/go1.go" `/type Date struct/` `/STOP/`}}
--->type Date struct {
+--> type Date struct {
month string
day int
}
</p>
<pre><!--{{code "progs/go1.go" `/STARTRUNE/` `/ENDRUNE/`}}
--->delta := 'δ' // delta has type rune.
+--> delta := 'δ' // delta has type rune.
var DELTA rune
DELTA = unicode.ToUpper(delta)
epsilon := unicode.ToLower(DELTA + 1)
</p>
<pre><!--{{code "progs/go1.go" `/delete\(m, k\)/`}}
--->delete(m, k)</pre>
+--> delete(m, k)</pre>
<p>
will delete the map entry retrieved by the expression <code>m[k]</code>.
</p>
<pre><!--{{code "progs/go1.go" `/Sunday/` `/^ }/`}}
--->m := map[string]int{"Sunday": 0, "Monday": 1}
+--> m := map[string]int{"Sunday": 0, "Monday": 1}
for name, value := range m {
// This loop should not assume Sunday will be visited first.
f(name, value)
</p>
<pre><!--{{code "progs/go1.go" `/sa :=/` `/then sc.0. = 2/`}}
--->sa := []int{1, 2, 3}
+--> sa := []int{1, 2, 3}
i := 0
i, sa[i] = 1, 2 // sets i = 1, sa[0] = 2
</p>
<pre><!--{{code "progs/go1.go" `/type Day struct/` `/Printf/`}}
--->type Day struct {
+--> type Day struct {
long string
short string
}
</p>
<pre><!--{{code "progs/go1.go" `/ErrSyntax/`}}
--->var ErrSyntax = errors.New("syntax error")</pre>
+--> var ErrSyntax = errors.New("syntax error")</pre>
<p>
<em>Updating</em>:
-->package main
import (
- "os"
"flag" // command line option parser
+ "os"
)
var omitNewline = flag.Bool("n", false, "don't print final newline")
There's one in the <code>for</code> clause on the next line:
<p>
<pre><!--{{code "progs/echo.go" `/for/`}}
--->for i := 0; i < flag.NArg(); i++ {</pre>
+--> for i := 0; i < flag.NArg(); i++ {</pre>
<p>
The <code>flag</code> package has parsed the arguments and left the non-flag arguments
in a list that can be iterated over in the obvious way.
reassigning it. This snippet from <code>strings.go</code> is legal code:
<p>
<pre><!--{{code "progs/strings.go" `/hello/` `/ciao/`}}
--->s := "hello"
+--> s := "hello"
if s[1] != 'e' {
os.Exit(1)
}
func (r13 *rotate13) String() string {
return r13.source.String()
-}
-// end of rotate13 implementation</pre>
+}</pre>
<p>
(The <code>rot13</code> function called in <code>Read</code> is trivial and not worth reproducing here.)
<p>
integer and can do the right thing for you. The snippet
<p>
<pre><!--{{code "progs/print.go" 10 11}}
--->var u64 uint64 = 1<<64 - 1
+--> var u64 uint64 = 1<<64 - 1
fmt.Printf("%d %d\n", u64, int64(u64))</pre>
<p>
prints
appropriate style, any value, even an array or structure. The output of
<p>
<pre><!--{{code "progs/print.go" 14 20}}
--->type T struct {
+--> type T struct {
a int
b string
}
to that of the <code>Printf</code> call above.
<p>
<pre><!--{{code "progs/print.go" 21 22}}
--->fmt.Print(u64, " ", t, " ", a, "\n")
+--> fmt.Print(u64, " ", t, " ", a, "\n")
fmt.Println(u64, t, a)</pre>
<p>
If you have your own type you'd like <code>Printf</code> or <code>Print</code> to format,
at the end of main:
<p>
<pre><!--{{code "progs/server1.go" `/adder,.quit/`}}
--->adder, quit := startServer(func(a, b int) int { return a + b })</pre>
+--> adder, quit := startServer(func(a, b int) int { return a + b })</pre>
...
<pre><!--{{code "progs/server1.go" `/quit....true/`}}
--->quit <- true</pre>
+--> quit <- true</pre>
<p>
There's a lot more to Go programming and concurrent programming in general but this
quick tour should give you some of the basics.
func (r13 *rotate13) String() string {
return r13.source.String()
}
-// end of rotate13 implementation
+
+// end of rotate13 implementation OMIT
func cat(r reader) {
const NBUF = 512
fmt.Println("Printing in g", i)
g(i + 1)
}
+
// STOP OMIT
// Revised version.
return io.Copy(dst, src)
}
+
// STOP OMIT
package main
import (
- "os"
"flag" // command line option parser
+ "os"
)
var omitNewline = flag.Bool("n", false, "don't print final newline")
func (e *errorString) Error() string {
return e.s
}
+
// STOP OMIT
// New returns an error that formats as the given text.
func New(text string) error {
return &errorString{text}
}
+
// STOP OMIT
func Sqrt(f float64) (float64, error) {
// implementation
return 0, nil // OMIT
}
+
// STOP OMIT
func printErr() (int, error) { // OMIT
func (f NegativeSqrtError) Error() string {
return fmt.Sprintf("math: square root of negative number %g", float64(f))
}
+
// STOP OMIT
type SyntaxError struct {
}
func (e *SyntaxError) Error() string { return e.msg }
+
// STOP OMIT
func decodeError(dec *json.Decoder, val struct{}) error { // OMIT
http.Error(w, err.Error(), 500)
}
}
+
// STOP OMIT
type ap struct{}
func init() {
http.Handle("/view", appHandler(viewRecord))
}
+
// STOP OMIT
func viewRecord(w http.ResponseWriter, r *http.Request) error {
}
return viewTemplate.Execute(w, record)
}
+
// STOP OMIT
type appHandler func(http.ResponseWriter, *http.Request) error
http.Error(w, err.Error(), 500)
}
}
+
// STOP OMIT
type ap struct{}
Message string
Code int
}
+
// STOP OMIT
type appHandler func(http.ResponseWriter, *http.Request) *appError
http.Error(w, e.Message, e.Code)
}
}
+
// STOP OMIT
func viewRecord(w http.ResponseWriter, r *http.Request) *appError {
}
return nil
}
+
// STOP OMIT
func init() {
func (se *SyntaxError) Error() string {
return fmt.Sprintf("%s:%d: %s", se.File, se.Line, se.Message)
}
+
// END ERROR EXAMPLE OMIT
func errorExample() {
package main
import (
- "fmt"
"./sort"
+ "fmt"
)
func ints() {
fmt.Printf("\n")
}
-
func main() {
ints()
strings()
return "", fmt.Errorf("incorrect code invocation: code %q %q", file, arg)
}
// Trim spaces from output.
- text = strings.TrimSpace(text)
+ text = strings.Trim(text, "\n")
// Replace tabs by spaces, which work better in HTML.
text = strings.Replace(text, "\t", " ", -1)
// Escape the program text for HTML.