<pre>
type T struct {
- name string; // name of the object
- value int; // its value
+ name string // name of the object
+ value int // its value
}
</pre>
<pre>
type T struct {
- name string; // name of the object
- value int; // its value
+ name string // name of the object
+ value int // its value
}
</pre>
</p>
<dl>
- <dt>Indentation</dt>
- <dd>We use tabs for indentation and <code>gofmt</code> emits them by default.
- Use spaces only if you must.
- </dd>
- <dt>Line length</dt>
- <dd>
- Go has no line length limit. Don't worry about overflowing a punched card.
- If a line feels too long, wrap it and indent with an extra tab.
- </dd>
- <dt>Parentheses</dt>
- <dd>
- Go needs fewer parentheses: control structures (<code>if</code>,
- <code>for</code>, <code>switch</code>) do not require parentheses in
- their syntax.
- Also, the operator precedence hierarchy is shorter and clearer, so
+ <dt>Indentation</dt>
+ <dd>We use tabs for indentation and <code>gofmt</code> emits them by default.
+ Use spaces only if you must.
+ </dd>
+ <dt>Line length</dt>
+ <dd>
+ Go has no line length limit. Don't worry about overflowing a punched card.
+ If a line feels too long, wrap it and indent with an extra tab.
+ </dd>
+ <dt>Parentheses</dt>
+ <dd>
+ Go needs fewer parentheses: control structures (<code>if</code>,
+ <code>for</code>, <code>switch</code>) do not require parentheses in
+ their syntax.
+ Also, the operator precedence hierarchy is shorter and clearer, so
<pre>
x<<8 + y<<16
</pre>
- means what the spacing implies.
- </dd>
+ means what the spacing implies.
+ </dd>
</dl>
<h2 id="commentary">Commentary</h2>
<pre>
/*
- The regexp package implements a simple library for
- regular expressions.
-
- The syntax of the regular expressions accepted is:
-
- regexp:
- concatenation { '|' concatenation }
- concatenation:
- { closure }
- closure:
- term [ '*' | '+' | '?' ]
- term:
- '^'
- '$'
- '.'
- character
- '[' [ '^' ] character-ranges ']'
- '(' regexp ')'
+ The regexp package implements a simple library for
+ regular expressions.
+
+ The syntax of the regular expressions accepted is:
+
+ regexp:
+ concatenation { '|' concatenation }
+ concatenation:
+ { closure }
+ closure:
+ term [ '*' | '+' | '?' ]
+ term:
+ '^'
+ '$'
+ '.'
+ character
+ '[' [ '^' ] character-ranges ']'
+ '(' regexp ')'
*/
package regexp
</pre>
<pre>
// Error codes returned by failures to parse an expression.
var (
- ErrInternal = os.NewError("internal error");
- ErrUnmatchedLpar = os.NewError("unmatched '('");
- ErrUnmatchedRpar = os.NewError("unmatched ')'");
- ...
+ ErrInternal = os.NewError("internal error")
+ ErrUnmatchedLpar = os.NewError("unmatched '('")
+ ErrUnmatchedRpar = os.NewError("unmatched ')'")
+ ...
)
</pre>
<pre>
var (
- countLock sync.Mutex;
- inputCount uint32;
- outputCount uint32;
- errorCount uint32;
+ countLock sync.Mutex
+ inputCount uint32
+ outputCount uint32
+ errorCount uint32
)
</pre>
<h2 id="semicolons">Semicolons</h2>
<p>
-Go needs fewer semicolons between statements than do other C variants.
-Semicolons are never required at the top level.
-And they are separators, not terminators, so they
-can be left off the last element of a statement or declaration list,
-a convenience
-for one-line <code>funcs</code> and the like.
+Like C, Go's formal grammar uses semicolons to terminate statements;
+unlike C, those semicolons do not appear in the source.
+Instead the lexer uses a simple rule to insert semicolons automatically
+as it scans, so the input text is mostly free of them.
</p>
+<p>
+The rule is this. If the last token before a newline is an identifier
+(which includes words like <code>int</code> and <code>float64</code>),
+a basic literal such as a number or string constant, or one of the
+tokens
+</p>
+<pre>
+break continue fallthrough return ++ -- ) }
+</pre>
+<p>
+the lexer always inserts a semicolon after the token.
+This could be summarized as, “if the newline comes
+after a token that could end a statement, add a semicolon”.
+</p>
+
+<p>
+A semicolon can also be omitted immediately before a closing brace,
+so a statement such as
+</p>
<pre>
-func CopyInBackground(dst, src chan Item) {
go func() { for { dst <- <-src } }()
-}
</pre>
+<p>
+needs no semicolons.
+Idiomatic Go programs have semicolons only in places such as
+<code>for</code> loop clauses, to separate the initializer, condition, and
+continuation elements. They are also necessary to separate multiple
+statements on a line, should you write code that way.
+</p>
<p>
-In fact, semicolons can be omitted at the end of any "StatementList" in the
-grammar, which includes things like cases in <code>switch</code>
-statements.
+One caveat. You should never put the opening brace of a
+control structure (<code>if</code>, <code>for</code>, <code>switch</code>,
+or <code>select</code>) on the next line. If you do, a semicolon
+will be inserted before the brace, which could cause unwanted
+effects. Write them like this
</p>
<pre>
-switch {
-case a < b:
- return -1
-case a == b:
- return 0
-case a > b:
- return 1
+if i < f() {
+ g()
}
-
</pre>
-
<p>
-The grammar accepts an empty statement after any statement list, which
-means a terminal semicolon is always OK. As a result,
-it's fine to put semicolons everywhere you'd put them in a
-C program—they would be fine after those return statements,
-for instance—but they can often be omitted.
-By convention, they're always left off top-level declarations (for
-instance, they don't appear after the closing brace of <code>struct</code>
-declarations, or of <code>funcs</code> for that matter)
-and often left off one-liners. But within functions, place them
-as you see fit.
+not like this
</p>
+<pre>
+if i < f() // wrong!
+{ // wrong!
+ g()
+}
+</pre>
+
<h2 id="control-structures">Control structures</h2>
<pre>
if err := file.Chmod(0664); err != nil {
- log.Stderr(err);
- return err;
+ log.Stderr(err)
+ return err
}
</pre>
</p>
<pre>
-f, err := os.Open(name, os.O_RDONLY, 0);
+f, err := os.Open(name, os.O_RDONLY, 0)
if err != nil {
- return err;
+ return err
}
-codeUsing(f);
+codeUsing(f)
</pre>
<p>
</p>
<pre>
-f, err := os.Open(name, os.O_RDONLY, 0);
+f, err := os.Open(name, os.O_RDONLY, 0)
if err != nil {
- return err;
+ return err
}
-d, err := f.Stat();
+d, err := f.Stat()
if err != nil {
- return err;
+ return err
}
-codeUsing(f, d);
+codeUsing(f, d)
</pre>
Short declarations make it easy to declare the index variable right in the loop.
</p>
<pre>
-sum := 0;
+sum := 0
for i := 0; i < 10; i++ {
sum += i
}
manage the loop for you.
</p>
<pre>
-var m map[string]int;
-sum := 0;
+var m map[string]int
+sum := 0
for _, value := range m { // key is unused
sum += value
}
<pre>
// Reverse a
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
- a[i], a[j] = a[j], a[i]
+ a[i], a[j] = a[j], a[i]
}
</pre>
<pre>
switch t := interfaceValue.(type) {
default:
- fmt.Printf("unexpected type %T", t); // %T prints type
+ fmt.Printf("unexpected type %T", t) // %T prints type
case bool:
- fmt.Printf("boolean %t\n", t);
+ fmt.Printf("boolean %t\n", t)
case int:
- fmt.Printf("integer %d\n", t);
+ fmt.Printf("integer %d\n", t)
case *bool:
- fmt.Printf("pointer to boolean %t\n", *t);
+ fmt.Printf("pointer to boolean %t\n", *t)
case *int:
- fmt.Printf("pointer to integer %d\n", *t);
+ fmt.Printf("pointer to integer %d\n", *t)
}
</pre>
<pre>
func nextInt(b []byte, i int) (int, int) {
- for ; i < len(b) && !isDigit(b[i]); i++ {
- }
- x := 0;
- for ; i < len(b) && isDigit(b[i]); i++ {
- x = x*10 + int(b[i])-'0'
- }
- return x, i;
+ for ; i < len(b) && !isDigit(b[i]); i++ {
+ }
+ x := 0
+ for ; i < len(b) && isDigit(b[i]); i++ {
+ x = x*10 + int(b[i])-'0'
+ }
+ return x, i
}
</pre>
</p>
<pre>
- for i := 0; i < len(a); {
- x, i = nextInt(a, i);
- fmt.Println(x);
- }
+ for i := 0; i < len(a); {
+ x, i = nextInt(a, i)
+ fmt.Println(x)
+ }
</pre>
<h3 id="named-results">Named result parameters</h3>
<pre>
func ReadFull(r Reader, buf []byte) (n int, err os.Error) {
- for len(buf) > 0 && err == nil {
- var nr int;
- nr, err = r.Read(buf);
- n += nr;
- buf = buf[nr:len(buf)];
- }
- return;
+ for len(buf) > 0 && err == nil {
+ var nr int
+ nr, err = r.Read(buf)
+ n += nr
+ buf = buf[nr:len(buf)]
+ }
+ return
}
</pre>
<pre>
type SyncedBuffer struct {
- lock sync.Mutex;
- buffer bytes.Buffer;
+ lock sync.Mutex
+ buffer bytes.Buffer
}
</pre>
</p>
<pre>
-p := new(SyncedBuffer); // type *SyncedBuffer
-var v SyncedBuffer; // type SyncedBuffer
+p := new(SyncedBuffer) // type *SyncedBuffer
+var v SyncedBuffer // type SyncedBuffer
</pre>
<h3 id="composite_literals">Constructors and composite literals</h3>
<pre>
func NewFile(fd int, name string) *File {
- if fd < 0 {
- return nil
- }
- f := new(File);
- f.fd = fd;
- f.name = name;
- f.dirinfo = nil;
- f.nepipe = 0;
- return f;
+ if fd < 0 {
+ return nil
+ }
+ f := new(File)
+ f.fd = fd
+ f.name = name
+ f.dirinfo = nil
+ f.nepipe = 0
+ return f
}
</pre>
<pre>
func NewFile(fd int, name string) *File {
- if fd < 0 {
- return nil
- }
- f := File{fd, name, nil, 0};
- return &f;
+ if fd < 0 {
+ return nil
+ }
+ f := File{fd, name, nil, 0}
+ return &f
}
</pre>
</p>
<pre>
- return &File{fd, name, nil, 0};
+ return &File{fd, name, nil, 0}
</pre>
<p>
</p>
<pre>
- return &File{fd: fd, name: name}
+ return &File{fd: fd, name: name}
</pre>
<p>
</p>
<pre>
-a := [...]string {Enone: "no error", Eio: "Eio", Einval: "invalid argument"};
-s := []string {Enone: "no error", Eio: "Eio", Einval: "invalid argument"};
-m := map[int]string{Enone: "no error", Eio: "Eio", Einval: "invalid argument"};
+a := [...]string {Enone: "no error", Eio: "Eio", Einval: "invalid argument"}
+s := []string {Enone: "no error", Eio: "Eio", Einval: "invalid argument"}
+m := map[int]string{Enone: "no error", Eio: "Eio", Einval: "invalid argument"}
</pre>
<h3 id="allocation_make">Allocation with <code>make()</code></h3>
</p>
<pre>
-var p *[]int = new([]int); // allocates slice structure; *p == nil; rarely useful
-var v []int = make([]int, 100); // v now refers to a new array of 100 ints
+var p *[]int = new([]int) // allocates slice structure; *p == nil; rarely useful
+var v []int = make([]int, 100) // v now refers to a new array of 100 ints
// Unnecessarily complex:
-var p *[]int = new([]int);
-*p = make([]int, 100, 100);
+var p *[]int = new([]int)
+*p = make([]int, 100, 100)
// Idiomatic:
-v := make([]int, 100);
+v := make([]int, 100)
</pre>
<p>
<pre>
func Sum(a *[3]float) (sum float) {
- for _, v := range *a {
- sum += v
- }
- return
+ for _, v := range *a {
+ sum += v
+ }
+ return
}
-array := [...]float{7.0, 8.5, 9.1};
-x := Sum(&array); // Note the explicit address-of operator
+array := [...]float{7.0, 8.5, 9.1}
+x := Sum(&array) // Note the explicit address-of operator
</pre>
<p>
<code>b</code>, <i>slice</i> (here used as a verb) the buffer.
</p>
<pre>
- n, err := f.Read(buf[0:32]);
+ n, err := f.Read(buf[0:32])
</pre>
<p>
Such slicing is common and efficient. In fact, leaving efficiency aside for
the moment, this snippet would also read the first 32 bytes of the buffer.
</p>
<pre>
- var n int;
- var err os.Error;
- for i := 0; i < 32; i++ {
- nbytes, e := f.Read(buf[i:i+1]); // Read one byte.
- if nbytes == 0 || e != nil {
- err = e;
- break;
- }
- n += nbytes;
- }
+ var n int
+ var err os.Error
+ for i := 0; i < 32; i++ {
+ nbytes, e := f.Read(buf[i:i+1]) // Read one byte.
+ if nbytes == 0 || e != nil {
+ err = e
+ break
+ }
+ n += nbytes
+ }
</pre>
<p>
The length of a slice may be changed as long as it still fits within
</p>
<pre>
func Append(slice, data[]byte) []byte {
- l := len(slice);
- if l + len(data) > cap(slice) { // reallocate
- // Allocate double what's needed, for future growth.
- newSlice := make([]byte, (l+len(data))*2);
- // Copy data (could use bytes.Copy()).
- for i, c := range slice {
- newSlice[i] = c
- }
- slice = newSlice;
- }
- slice = slice[0:l+len(data)];
- for i, c := range data {
- slice[l+i] = c
- }
- return slice;
+ l := len(slice)
+ if l + len(data) > cap(slice) { // reallocate
+ // Allocate double what's needed, for future growth.
+ newSlice := make([]byte, (l+len(data))*2)
+ // Copy data (could use bytes.Copy()).
+ for i, c := range slice {
+ newSlice[i] = c
+ }
+ slice = newSlice
+ }
+ slice = slice[0:l+len(data)]
+ for i, c := range data {
+ slice[l+i] = c
+ }
+ return slice
}
</pre>
<p>
</p>
<pre>
var timeZone = map[string] int {
- "UTC": 0*60*60,
- "EST": -5*60*60,
- "CST": -6*60*60,
- "MST": -7*60*60,
- "PST": -8*60*60,
+ "UTC": 0*60*60,
+ "EST": -5*60*60,
+ "CST": -6*60*60,
+ "MST": -7*60*60,
+ "PST": -8*60*60,
}
</pre>
<p>
there is a way to do so safely using a multiple assignment.
</p>
<pre>
-var seconds int;
-var ok bool;
+var seconds int
+var ok bool
seconds, ok = timeZone[tz]
</pre>
<p>
</p>
<pre>
func offset(tz string) int {
- if seconds, ok := timeZone[tz]; ok {
- return seconds
- }
- log.Stderr("unknown time zone", tz);
- return 0;
+ if seconds, ok := timeZone[tz]; ok {
+ return seconds
+ }
+ log.Stderr("unknown time zone", tz)
+ return 0
}
</pre>
<p>
identifier in place of the usual variable for the value.
</p>
<pre>
-_, present := timeZone[tz];
+_, present := timeZone[tz]
</pre>
<p>
To delete a map entry, turn the multiple assignment around by placing
from the map.
</p>
<pre>
-timeZone["PDT"] = 0, false; // Now on Standard Time
+timeZone["PDT"] = 0, false // Now on Standard Time
</pre>
<h3 id="printing">Printing</h3>
In this example each line produces the same output.
</p>
<pre>
-fmt.Printf("Hello %d\n", 23);
-fmt.Fprint(os.Stdout, "Hello ", 23, "\n");
-fmt.Println(fmt.Sprint("Hello ", 23));
+fmt.Printf("Hello %d\n", 23)
+fmt.Fprint(os.Stdout, "Hello ", 23, "\n")
+fmt.Println(fmt.Sprint("Hello ", 23))
</pre>
<p>
As mentioned in
type of the argument to decide these properties.
</p>
<pre>
-var x uint64 = 1<<64 - 1;
-fmt.Printf("%d %x; %d %x\n", x, x, int64(x), int64(x));
+var x uint64 = 1<<64 - 1
+fmt.Printf("%d %x; %d %x\n", x, x, int64(x), int64(x))
</pre>
<p>
prints
maps. Here is a print statement for the time zone map defined in the previous section.
</p>
<pre>
-fmt.Printf("%v\n", timeZone); // or just fmt.Println(timeZone);
+fmt.Printf("%v\n", timeZone) // or just fmt.Println(timeZone)
</pre>
<p>
which gives output
</p>
<pre>
type T struct {
- a int;
- b float;
- c string;
+ a int
+ b float
+ c string
}
-t := &T{ 7, -2.35, "abc\tdef" };
-fmt.Printf("%v\n", t);
-fmt.Printf("%+v\n", t);
-fmt.Printf("%#v\n", t);
-fmt.Printf("%#v\n", timeZone);
+t := &T{ 7, -2.35, "abc\tdef" }
+fmt.Printf("%v\n", t)
+fmt.Printf("%+v\n", t)
+fmt.Printf("%#v\n", t)
+fmt.Printf("%#v\n", timeZone)
</pre>
<p>
prints
<p>
Another handy format is <code>%T</code>, which prints the <em>type</em> of a value.
<pre>
-fmt.Printf("%T\n", timeZone);
+fmt.Printf("%T\n", timeZone)
</pre>
<p>
prints
</p>
<pre>
func (t *T) String() string {
- return fmt.Sprintf("%d/%g/%q", t.a, t.b, t.c);
+ return fmt.Sprintf("%d/%g/%q", t.a, t.b, t.c)
}
-fmt.Printf("%v\n", t);
+fmt.Printf("%v\n", t)
</pre>
<p>
to print in the format
<pre>
// Stderr is a helper function for easy logging to stderr. It is analogous to Fprint(os.Stderr).
func Stderr(v ...) {
- stderr.Output(2, fmt.Sprintln(v)); // Output takes parameters (int, string)
+ stderr.Output(2, fmt.Sprintln(v)) // Output takes parameters (int, string)
}
</pre>
<p>
<pre>
type ByteSize float64
const (
- _ = iota; // ignore first value by assigning to blank identifier
- KB ByteSize = 1<<(10*iota);
- MB;
- GB;
- TB;
- PB;
- YB;
+ _ = iota // ignore first value by assigning to blank identifier
+ KB ByteSize = 1<<(10*iota)
+ MB
+ GB
+ TB
+ PB
+ YB
)
</pre>
<p>
</p>
<pre>
func (b ByteSize) String() string {
- switch {
- case b >= YB:
- return fmt.Sprintf("%.2fYB", b/YB)
- case b >= PB:
- return fmt.Sprintf("%.2fPB", b/PB)
- case b >= TB:
- return fmt.Sprintf("%.2fTB", b/TB)
- case b >= GB:
- return fmt.Sprintf("%.2fGB", b/GB)
- case b >= MB:
- return fmt.Sprintf("%.2fMB", b/MB)
- case b >= KB:
- return fmt.Sprintf("%.2fKB", b/KB)
- }
- return fmt.Sprintf("%.2fB", b)
+ switch {
+ case b >= YB:
+ return fmt.Sprintf("%.2fYB", b/YB)
+ case b >= PB:
+ return fmt.Sprintf("%.2fPB", b/PB)
+ case b >= TB:
+ return fmt.Sprintf("%.2fTB", b/TB)
+ case b >= GB:
+ return fmt.Sprintf("%.2fGB", b/GB)
+ case b >= MB:
+ return fmt.Sprintf("%.2fMB", b/MB)
+ case b >= KB:
+ return fmt.Sprintf("%.2fKB", b/KB)
+ }
+ return fmt.Sprintf("%.2fB", b)
}
</pre>
<p>
</p>
<pre>
var (
- HOME = os.Getenv("HOME");
- USER = os.Getenv("USER");
- GOROOT = os.Getenv("GOROOT");
+ HOME = os.Getenv("HOME")
+ USER = os.Getenv("USER")
+ GOROOT = os.Getenv("GOROOT")
)
</pre>
<pre>
func init() {
- if USER == "" {
- log.Exit("$USER not set")
- }
- if HOME == "" {
- HOME = "/usr/" + USER
- }
- if GOROOT == "" {
- GOROOT = HOME + "/go"
- }
- // GOROOT may be overridden by --goroot flag on command line.
- flag.StringVar(&GOROOT, "goroot", GOROOT, "Go root directory")
+ if USER == "" {
+ log.Exit("$USER not set")
+ }
+ if HOME == "" {
+ HOME = "/usr/" + USER
+ }
+ if GOROOT == "" {
+ GOROOT = HOME + "/go"
+ }
+ // GOROOT may be overridden by --goroot flag on command line.
+ flag.StringVar(&GOROOT, "goroot", GOROOT, "Go root directory")
}
</pre>
type ByteSlice []byte
func (slice ByteSlice) Append(data []byte) []byte {
- // Body exactly the same as above
+ // Body exactly the same as above
}
</pre>
<p>
</p>
<pre>
func (p *ByteSlice) Append(data []byte) {
- slice := *p;
- // Body as above, without the return.
- *p = slice;
+ slice := *p
+ // Body as above, without the return.
+ *p = slice
}
</pre>
<p>
</p>
<pre>
func (p *ByteSlice) Write(data []byte) (n int, err os.Error) {
- slice := *p;
- // Again as above.
- *p = slice;
- return len(data), nil;
+ slice := *p
+ // Again as above.
+ *p = slice
+ return len(data), nil
}
</pre>
<p>
print into one.
</p>
<pre>
- var b ByteSlice;
- fmt.Fprintf(&b, "This hour has %d days\n", 7);
+ var b ByteSlice
+ fmt.Fprintf(&b, "This hour has %d days\n", 7)
</pre>
<p>
We pass the address of a <code>ByteSlice</code>
// Methods required by sort.Interface.
func (s Sequence) Len() int {
- return len(s)
+ return len(s)
}
func (s Sequence) Less(i, j int) bool {
- return s[i] < s[j]
+ return s[i] < s[j]
}
func (s Sequence) Swap(i, j int) {
- s[i], s[j] = s[j], s[i]
+ s[i], s[j] = s[j], s[i]
}
// Method for printing - sorts the elements before printing.
func (s Sequence) String() string {
- sort.Sort(s);
- str := "[";
- for i, elem := range s {
- if i > 0 {
- str += " "
- }
- str += fmt.Sprint(elem);
- }
- return str + "]";
+ sort.Sort(s)
+ str := "["
+ for i, elem := range s {
+ if i > 0 {
+ str += " "
+ }
+ str += fmt.Sprint(elem)
+ }
+ return str + "]"
}
</pre>
</p>
<pre>
func (s Sequence) String() string {
- sort.Sort(s);
- return fmt.Sprint([]int(s));
+ sort.Sort(s)
+ return fmt.Sprint([]int(s))
}
</pre>
<p>
// Method for printing - sorts the elements before printing
func (s Sequence) String() string {
- sort.IntArray(s).Sort();
- return fmt.Sprint([]int(s))
+ sort.IntArray(s).Sort()
+ return fmt.Sprint([]int(s))
}
</pre>
<p>
</p>
<pre>
type Cipher interface {
- BlockSize() int;
- Encrypt(src, dst []byte);
- Decrypt(src, dst []byte);
+ BlockSize() int
+ Encrypt(src, dst []byte)
+ Decrypt(src, dst []byte)
}
// NewECBDecrypter returns a reader that reads data
</p>
<pre>
type Handler interface {
- ServeHTTP(*Conn, *Request);
+ ServeHTTP(*Conn, *Request)
}
</pre>
<p>
<pre>
// Simple counter server.
type Counter struct {
- n int;
+ n int
}
func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
- ctr.n++;
- fmt.Fprintf(c, "counter = %d\n", ctr.n);
+ ctr.n++
+ fmt.Fprintf(c, "counter = %d\n", ctr.n)
}
</pre>
<p>
<pre>
import "http"
...
-ctr := new(Counter);
-http.Handle("/counter", ctr);
+ctr := new(Counter)
+http.Handle("/counter", ctr)
</pre>
<p>
But why make <code>Counter</code> a struct? An integer is all that's needed.
type Counter int
func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
- *ctr++;
- fmt.Fprintf(c, "counter = %d\n", *ctr);
+ *ctr++
+ fmt.Fprintf(c, "counter = %d\n", *ctr)
}
</pre>
<p>
type Chan chan *http.Request
func (ch Chan) ServeHTTP(c *http.Conn, req *http.Request) {
- ch <- req;
- fmt.Fprint(c, "notification sent");
+ ch <- req
+ fmt.Fprint(c, "notification sent")
}
</pre>
<p>
</p>
<pre>
func ArgServer() {
- for i, s := range os.Args {
- fmt.Println(s);
- }
+ for i, s := range os.Args {
+ fmt.Println(s)
+ }
}
</pre>
<p>
// ServeHTTP calls f(c, req).
func (f HandlerFunc) ServeHTTP(c *Conn, req *Request) {
- f(c, req);
+ f(c, req)
}
</pre>
<p>
<pre>
// Argument server.
func ArgServer(c *http.Conn, req *http.Request) {
- for i, s := range os.Args {
- fmt.Fprintln(c, s);
- }
+ for i, s := range os.Args {
+ fmt.Fprintln(c, s)
+ }
}
</pre>
<p>
The code to set it up is concise:
</p>
<pre>
-http.Handle("/args", http.HandlerFunc(ArgServer));
+http.Handle("/args", http.HandlerFunc(ArgServer))
</pre>
<p>
When someone visits the page <code>/args</code>,
</p>
<pre>
type Reader interface {
- Read(p []byte) (n int, err os.Error);
+ Read(p []byte) (n int, err os.Error)
}
type Writer interface {
- Write(p []byte) (n int, err os.Error);
+ Write(p []byte) (n int, err os.Error)
}
</pre>
<p>
<pre>
// ReadWrite is the interface that groups the basic Read and Write methods.
type ReadWriter interface {
- Reader;
- Writer;
+ Reader
+ Writer
}
</pre>
<p>
// ReadWriter stores pointers to a Reader and a Writer.
// It implements io.ReadWriter.
type ReadWriter struct {
- *Reader;
- *Writer;
+ *Reader
+ *Writer
}
</pre>
<p>
</p>
<pre>
type ReadWriter struct {
- reader *Reader;
- writer *Writer;
+ reader *Reader
+ writer *Writer
}
</pre>
<p>
</p>
<pre>
func (rw *ReadWriter) Read(p []byte) (n int, err os.Error) {
- return rw.reader.Read(p)
+ return rw.reader.Read(p)
}
</pre>
<p>
</p>
<pre>
type Job struct {
- Command string;
- *log.Logger;
+ Command string
+ *log.Logger
}
</pre>
<p>
log to a <code>Job</code>:
</p>
<pre>
-job.Log("starting now...");
+job.Log("starting now...")
</pre>
<p>
The <code>Logger</code> is a regular field of the struct and we can initialize
</p>
<pre>
func NewJob(command string, logger *log.Logger) *Job {
- return &Job{command, logger}
+ return &Job{command, logger}
}
</pre>
<p>
</p>
<pre>
func (job *Job) Logf(format string, args ...) {
- job.Logger.Logf("%q: %s", job.Command, fmt.Sprintf(format, args));
+ job.Logger.Logf("%q: %s", job.Command, fmt.Sprintf(format, args))
}
</pre>
<p>
background.)
</p>
<pre>
-go list.Sort(); // run list.Sort in parallel; don't wait for it.
+go list.Sort() // run list.Sort in parallel; don't wait for it.
</pre>
<p>
A function literal can be handy in a goroutine invocation.
<pre>
func Announce(message string, delay int64) {
- go func() {
- time.Sleep(delay);
- fmt.Println(message);
- }() // Note the parentheses - must call the function.
+ go func() {
+ time.Sleep(delay)
+ fmt.Println(message)
+ }() // Note the parentheses - must call the function.
}
</pre>
<p>
The default is zero, for an unbuffered or synchronous channel.
</p>
<pre>
-ci := make(chan int); // unbuffered channel of integers
-cj := make(chan int, 0); // unbuffered channel of integers
-cs := make(chan *os.File, 100); // buffered channel of pointers to Files
+ci := make(chan int) // unbuffered channel of integers
+cj := make(chan int, 0) // unbuffered channel of integers
+cs := make(chan *os.File, 100) // buffered channel of pointers to Files
</pre>
<p>
Channels combine communication—the exchange of a value—with
can allow the launching goroutine to wait for the sort to complete.
</p>
<pre>
-c := make(chan int); // Allocate a channel.
+c := make(chan int) // Allocate a channel.
// Start the sort in a goroutine; when it completes, signal on the channel.
go func() {
- list.Sort();
- c <- 1; // Send a signal; value does not matter.
-}();
-doSomethingForAWhile();
-<-c; // Wait for sort to finish; discard sent value.
+ list.Sort()
+ c <- 1 // Send a signal; value does not matter.
+}()
+doSomethingForAWhile()
+<-c // Wait for sort to finish; discard sent value.
</pre>
<p>
Receivers always block until there is data to receive.
var sem = make(chan int, MaxOutstanding)
func handle(r *Request) {
- sem <- 1; // Wait for active queue to drain.
- process(r); // May take a long time.
- <-sem; // Done; enable next request to run.
+ sem <- 1 // Wait for active queue to drain.
+ process(r) // May take a long time.
+ <-sem // Done; enable next request to run.
}
func Serve(queue chan *Request) {
for {
- req := <-queue;
- go handle(req); // Don't wait for handle to finish.
+ req := <-queue
+ go handle(req) // Don't wait for handle to finish.
}
}
</pre>
</p>
<pre>
func handle(queue chan *Request) {
- for r := range queue {
- process(r);
- }
+ for r := range queue {
+ process(r)
+ }
}
func Serve(clientRequests chan *clientRequests, quit chan bool) {
- // Start handlers
- for i := 0; i < MaxOutstanding; i++ {
- go handle(clientRequests)
- }
- <-quit; // Wait to be told to exit.
+ // Start handlers
+ for i := 0; i < MaxOutstanding; i++ {
+ go handle(clientRequests)
+ }
+ <-quit // Wait to be told to exit.
}
</pre>
</p>
<pre>
type Request struct {
- args []int;
- f func([]int) int;
- resultChan chan int;
+ args []int
+ f func([]int) int
+ resultChan chan int
}
</pre>
<p>
</p>
<pre>
func sum(a []int) (s int) {
- for _, v := range a {
- s += v
- }
- return
+ for _, v := range a {
+ s += v
+ }
+ return
}
request := &Request{[]int{3, 4, 5}, sum, make(chan int)}
// Send request
-clientRequests <- request;
+clientRequests <- request
// Wait for response.
-fmt.Printf("answer: %d\n", <-request.resultChan);
+fmt.Printf("answer: %d\n", <-request.resultChan)
</pre>
<p>
On the server side, the handler function is the only thing that changes.
</p>
<pre>
func handle(queue chan *Request) {
- for req := range queue {
- req.resultChan <- req.f(req.args);
- }
+ for req := range queue {
+ req.resultChan <- req.f(req.args)
+ }
}
</pre>
<p>
for ; i < n; i++ {
v[i] += u.Op(v[i])
}
- c <- 1; // signal that this piece is done
+ c <- 1 // signal that this piece is done
}
</pre>
<p>
launching all the goroutines.
</p>
<pre>
-const NCPU = 4 // number of CPU cores
+const NCPU = 4 // number of CPU cores
func (v Vector) DoAll(u Vector) {
- c := make(chan int, NCPU); // Buffering optional but sensible.
+ c := make(chan int, NCPU) // Buffering optional but sensible.
for i := 0; i < NCPU; i++ {
- go v.DoSome(i*len(v)/NCPU, (i+1)*len(v)/NCPU, u, c);
+ go v.DoSome(i*len(v)/NCPU, (i+1)*len(v)/NCPU, u, c)
}
// Drain the channel.
for i := 0; i < NCPU; i++ {
var serverChan = make(chan *Buffer)
func client() {
- for {
- b, ok := <-freeList; // grab a buffer if available
- if !ok { // if not, allocate a new one
- b = new(Buffer)
- }
- load(b); // read next message from the net
- serverChan <- b; // send to server
- }
+ for {
+ b, ok := <-freeList // grab a buffer if available
+ if !ok { // if not, allocate a new one
+ b = new(Buffer)
+ }
+ load(b) // read next message from the net
+ serverChan <- b // send to server
+ }
}
</pre>
<p>
</p>
<pre>
func server() {
- for {
- b := <-serverChan; // wait for work
- process(b);
- _ = freeList <- b; // reuse buffer if room
- }
+ for {
+ b := <-serverChan // wait for work
+ process(b)
+ _ = freeList <- b // reuse buffer if room
+ }
}
</pre>
<p>
</p>
<pre>
type Error interface {
- String() string;
+ String() string
}
</pre>
<p>
// PathError records an error and the operation and
// file path that caused it.
type PathError struct {
- Op string; // "open", "unlink", etc.
- Path string; // The associated file.
- Error Error; // Returned by the system call.
+ Op string // "open", "unlink", etc.
+ Path string // The associated file.
+ Error Error // Returned by the system call.
}
func (e *PathError) String() string {
- return e.Op + " " + e.Path + ": " + e.Error.String();
+ return e.Op + " " + e.Path + ": " + e.Error.String()
}
</pre>
<p>
<pre>
for try := 0; try < 2; try++ {
- file, err = os.Open(filename, os.O_RDONLY, 0);
- if err == nil {
- return
- }
- if e, ok := err.(*os.PathError); ok && e.Error == os.ENOSPC {
- deleteTempFiles(); // Recover some space.
- continue
- }
- return
+ file, err = os.Open(filename, os.O_RDONLY, 0)
+ if err == nil {
+ return
+ }
+ if e, ok := err.(*os.PathError); ok && e.Error == os.ENOSPC {
+ deleteTempFiles() // Recover some space.
+ continue
+ }
+ return
}
</pre>
package main
import (
- "flag";
- "http";
- "io";
- "log";
- "strings";
- "template";
+ "flag"
+ "http"
+ "io"
+ "log"
+ "strings"
+ "template"
)
var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
var fmap = template.FormatterMap{
- "html": template.HTMLFormatter,
- "url+html": UrlHtmlFormatter,
+ "html": template.HTMLFormatter,
+ "url+html": UrlHtmlFormatter,
}
var templ = template.MustParse(templateStr, fmap)
func main() {
- flag.Parse();
- http.Handle("/", http.HandlerFunc(QR));
- err := http.ListenAndServe(*addr, nil);
- if err != nil {
- log.Exit("ListenAndServe:", err);
- }
+ flag.Parse()
+ http.Handle("/", http.HandlerFunc(QR))
+ err := http.ListenAndServe(*addr, nil)
+ if err != nil {
+ log.Exit("ListenAndServe:", err)
+ }
}
func QR(c *http.Conn, req *http.Request) {
- templ.Execute(req.FormValue("s"), c);
+ templ.Execute(req.FormValue("s"), c)
}
func UrlHtmlFormatter(w io.Writer, v interface{}, fmt string) {
- template.HTMLEscape(w, strings.Bytes(http.URLEscape(v.(string))));
+ template.HTMLEscape(w, strings.Bytes(http.URLEscape(v.(string))))
}
<!--
TODO
-<pre>
-verifying implementation
-type Color uint32
-
-// Check that Color implements image.Color and image.Image
-var _ image.Color = Black
-var _ image.Image = Black
-</pre>
+<pre>
+verifying implementation
+type Color uint32
+
+// Check that Color implements image.Color and image.Image
+var _ image.Color = Black
+var _ image.Image = Black
+</pre>
-->