</p>
<pre>
-if i < f() {
+if i < f() {
g()
}
</pre>
not like this
</p>
<pre>
-if i < f() // wrong!
+if i < f() // wrong!
{ // wrong!
g()
}
In Go a simple <code>if</code> looks like this:
</p>
<pre>
-if x > 0 {
+if x > 0 {
return y
}
</pre>
</p>
<pre>
sum := 0
-for i := 0; i < 10; i++ {
+for i := 0; i < 10; i++ {
sum += i
}
</pre>
</p>
<pre>
// Reverse a
-for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
+for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
a[i], a[j] = a[j], a[i]
}
</pre>
<pre>
func nextInt(b []byte, i int) (int, int) {
- for ; i < len(b) && !isDigit(b[i]); i++ {
+ for ; i < len(b) && !isDigit(b[i]); i++ {
}
x := 0
- for ; i < len(b) && isDigit(b[i]); i++ {
+ for ; i < len(b) && isDigit(b[i]); i++ {
x = x*10 + int(b[i])-'0'
}
return x, i
</p>
<pre>
- for i := 0; i < len(a); {
+ for i := 0; i < len(a); {
x, i = nextInt(a, i)
fmt.Println(x)
}
<pre>
func ReadFull(r Reader, buf []byte) (n int, err os.Error) {
- for len(buf) > 0 && err == nil {
+ for len(buf) > 0 && err == nil {
var nr int
nr, err = r.Read(buf)
n += nr
<pre>
var n int
var err os.Error
- for i := 0; i < 32; i++ {
+ for i := 0; i < 32; i++ {
nbytes, e := f.Read(buf[i:i+1]) // Read one byte.
if nbytes == 0 || e != nil {
err = e
<pre>
func Append(slice, data[]byte) []byte {
l := len(slice)
- if l + len(data) > cap(slice) { // reallocate
+ 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()).
type of the argument to decide these properties.
</p>
<pre>
-var x uint64 = 1<<64 - 1
+var x uint64 = 1<<64 - 1
fmt.Printf("%d %x; %d %x\n", x, x, int64(x), int64(x))
</pre>
<p>
type ByteSize float64
const (
_ = iota // ignore first value by assigning to blank identifier
- KB ByteSize = 1<<(10*iota)
+ KB ByteSize = 1<<(10*iota)
MB
GB
TB
<pre>
func (b ByteSize) String() string {
switch {
- case b >= YB:
+ case b >= YB:
return fmt.Sprintf("%.2fYB", b/YB)
- case b >= PB:
+ case b >= PB:
return fmt.Sprintf("%.2fPB", b/PB)
- case b >= TB:
+ case b >= TB:
return fmt.Sprintf("%.2fTB", b/TB)
- case b >= GB:
+ case b >= GB:
return fmt.Sprintf("%.2fGB", b/GB)
- case b >= MB:
+ case b >= MB:
return fmt.Sprintf("%.2fMB", b/MB)
- case b >= KB:
+ case b >= KB:
return fmt.Sprintf("%.2fKB", b/KB)
}
return fmt.Sprintf("%.2fB", b)
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]
sort.Sort(s)
str := "["
for i, elem := range s {
- if i > 0 {
+ if i > 0 {
str += " "
}
str += fmt.Sprint(elem)
type Chan chan *http.Request
func (ch Chan) ServeHTTP(c *http.Conn, req *http.Request) {
- ch <- req
+ ch <- req
fmt.Fprint(c, "notification sent")
}
</pre>
var sem = make(chan int, MaxOutstanding)
func handle(r *Request) {
- sem <- 1 // Wait for active queue to drain.
+ sem <- 1 // Wait for active queue to drain.
process(r) // May take a long time.
- <-sem // Done; enable next request to run.
+ <-sem // Done; enable next request to run.
}
func Serve(queue chan *Request) {
for {
- req := <-queue
+ req := <-queue
go handle(req) // Don't wait for handle to finish.
}
}
func Serve(clientRequests chan *clientRequests, quit chan bool) {
// Start handlers
- for i := 0; i < MaxOutstanding; i++ {
+ for i := 0; i < MaxOutstanding; i++ {
go handle(clientRequests)
}
- <-quit // Wait to be told to exit.
+ <-quit // Wait to be told to exit.
}
</pre>
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.
<pre>
func handle(queue chan *Request) {
for req := range queue {
- req.resultChan <- req.f(req.args)
+ req.resultChan <- req.f(req.args)
}
}
</pre>
// Apply the operation to v[i], v[i+1] ... up to v[n-1].
func (v Vector) DoSome(i, n int, u Vector, c chan int) {
- for ; i < n; i++ {
+ 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>
func (v Vector) DoAll(u Vector) {
c := make(chan int, NCPU) // Buffering optional but sensible.
- for i := 0; i < NCPU; i++ {
+ for i := 0; i < NCPU; i++ {
go v.DoSome(i*len(v)/NCPU, (i+1)*len(v)/NCPU, u, c)
}
// Drain the channel.
- for i := 0; i < NCPU; i++ {
- <-c // wait for one task to complete
+ for i := 0; i < NCPU; i++ {
+ <-c // wait for one task to complete
}
// All done.
}
func client() {
for {
- b, ok := <-freeList // grab a buffer if available
+ 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
+ serverChan <- b // send to server
}
}
</pre>
<pre>
func server() {
for {
- b := <-serverChan // wait for work
+ b := <-serverChan // wait for work
process(b)
- _ = freeList <- b // reuse buffer if room
+ _ = freeList <- b // reuse buffer if room
}
}
</pre>
</p>
<pre>
-for try := 0; try < 2; try++ {
+for try := 0; try < 2; try++ {
file, err = os.Open(filename, os.O_RDONLY, 0)
if err == nil {
return