import (
"os"
- "flag" // command line option parser
+ "flag" // command line option parser
)
var omitNewline = flag.Bool("n", false, "don't print final newline")
const (
- Space = " "
+ Space = " "
Newline = "\n"
)
func main() {
- flag.Parse() // Scans the arg list and sets up flags
+ flag.Parse() // Scans the arg list and sets up flags
var s string = ""
for i := 0; i < flag.NArg(); i++ {
if i > 0 {
)
type File struct {
- fd syscall.Handle // file descriptor number
- name string // file name at Open time
+ fd syscall.Handle // file descriptor number
+ name string // file name at Open time
}
func newFile(fd syscall.Handle, name string) *File {
package main
-import fmt "fmt" // Package implementing formatted I/O.
+import fmt "fmt" // Package implementing formatted I/O.
func main() {
fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n")
file.Stdout.Write(hello)
f, err := file.Open("/does/not/exist")
if f == nil {
- fmt.Printf("can't open file; err=%s\n", err.String())
+ fmt.Printf("can't open file; err=%s\n", err.String())
os.Exit(1)
}
}
import "fmt"
func main() {
- var u64 uint64 = 1<<64-1
+ var u64 uint64 = 1<<64 - 1
fmt.Printf("%d %d\n", u64, int64(u64))
// harder stuff
import "fmt"
type request struct {
- a, b int
- replyc chan int
+ a, b int
+ replyc chan int
}
type binOp func(a, b int) int
func server(op binOp, service chan *request) {
for {
req := <-service
- go run(op, req) // don't wait for it
+ go run(op, req) // don't wait for it
}
}
req.replyc = make(chan int)
adder <- req
}
- for i := N-1; i >= 0; i-- { // doesn't matter what order
- if <-reqs[i].replyc != N + 2*i {
+ for i := N - 1; i >= 0; i-- { // doesn't matter what order
+ if <-reqs[i].replyc != N+2*i {
fmt.Println("fail at", i)
}
}
import "fmt"
type request struct {
- a, b int
- replyc chan int
+ a, b int
+ replyc chan int
}
type binOp func(a, b int) int
for {
select {
case req := <-service:
- go run(op, req) // don't wait for it
+ go run(op, req) // don't wait for it
case <-quit:
return
}
req.replyc = make(chan int)
adder <- req
}
- for i := N-1; i >= 0; i-- { // doesn't matter what order
- if <-reqs[i].replyc != N + 2*i {
+ for i := N - 1; i >= 0; i-- { // doesn't matter what order
+ if <-reqs[i].replyc != N+2*i {
fmt.Println("fail at", i)
}
}
// Send the sequence 2, 3, 4, ... to channel 'ch'.
func generate(ch chan int) {
for i := 2; ; i++ {
- ch <- i // Send 'i' to channel 'ch'.
+ ch <- i // Send 'i' to channel 'ch'.
}
}
// removing those divisible by 'prime'.
func filter(in, out chan int, prime int) {
for {
- i := <-in // Receive value of new variable 'i' from 'in'.
- if i % prime != 0 {
- out <- i // Send 'i' to channel 'out'.
+ i := <-in // Receive value of new variable 'i' from 'in'.
+ if i%prime != 0 {
+ out <- i // Send 'i' to channel 'out'.
}
}
}
// The prime sieve: Daisy-chain filter processes together.
func main() {
- ch := make(chan int) // Create a new channel.
- go generate(ch) // Start generate() as a goroutine.
+ ch := make(chan int) // Create a new channel.
+ go generate(ch) // Start generate() as a goroutine.
for i := 0; i < 100; i++ { // Print the first hundred primes.
prime := <-ch
fmt.Println(prime)
// Send the sequence 2, 3, 4, ... to returned channel
func generate() chan int {
ch := make(chan int)
- go func(){
+ go func() {
for i := 2; ; i++ {
ch <- i
}
out := make(chan int)
go func() {
for {
- if i := <-in; i % prime != 0 {
+ if i := <-in; i%prime != 0 {
out <- i
}
}
func IsSorted(data Interface) bool {
n := data.Len()
for i := n - 1; i > 0; i-- {
- if data.Less(i, i - 1) {
+ if data.Less(i, i-1) {
return false
}
}
type IntSlice []int
-func (p IntSlice) Len() int { return len(p) }
-func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
-func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
-
+func (p IntSlice) Len() int { return len(p) }
+func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
+func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type Float64Slice []float64
-func (p Float64Slice) Len() int { return len(p) }
-func (p Float64Slice) Less(i, j int) bool { return p[i] < p[j] }
-func (p Float64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
-
+func (p Float64Slice) Len() int { return len(p) }
+func (p Float64Slice) Less(i, j int) bool { return p[i] < p[j] }
+func (p Float64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type StringSlice []string
-func (p StringSlice) Len() int { return len(p) }
-func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
-func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
-
+func (p StringSlice) Len() int { return len(p) }
+func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
+func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Convenience wrappers for common cases
-func SortInts(a []int) { Sort(IntSlice(a)) }
-func SortFloat64s(a []float64) { Sort(Float64Slice(a)) }
-func SortStrings(a []string) { Sort(StringSlice(a)) }
-
+func SortInts(a []int) { Sort(IntSlice(a)) }
+func SortFloat64s(a []float64) { Sort(Float64Slice(a)) }
+func SortStrings(a []string) { Sort(StringSlice(a)) }
-func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) }
-func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) }
-func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) }
+func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) }
+func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) }
+func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) }
}
type day struct {
- num int
- shortName string
- longName string
+ num int
+ shortName string
+ longName string
}
type dayArray struct {
data []*day
}
-func (p *dayArray) Len() int { return len(p.data) }
-func (p *dayArray) Less(i, j int) bool { return p.data[i].num < p.data[j].num }
-func (p *dayArray) Swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i] }
+func (p *dayArray) Len() int { return len(p.data) }
+func (p *dayArray) Less(i, j int) bool { return p.data[i].num < p.data[j].num }
+func (p *dayArray) Swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i] }
func days() {
- Sunday := day{0, "SUN", "Sunday"}
- Monday := day{1, "MON", "Monday"}
- Tuesday := day{2, "TUE", "Tuesday"}
+ Sunday := day{0, "SUN", "Sunday"}
+ Monday := day{1, "MON", "Monday"}
+ Tuesday := day{2, "TUE", "Tuesday"}
Wednesday := day{3, "WED", "Wednesday"}
- Thursday := day{4, "THU", "Thursday"}
- Friday := day{5, "FRI", "Friday"}
- Saturday := day{6, "SAT", "Saturday"}
+ Thursday := day{4, "THU", "Thursday"}
+ Friday := day{5, "FRI", "Friday"}
+ Saturday := day{6, "SAT", "Saturday"}
data := []*day{&Tuesday, &Thursday, &Wednesday, &Sunday, &Monday, &Friday, &Saturday}
a := dayArray{data}
sort.Sort(&a)
func main() {
s := "hello"
- if s[1] != 'e' { os.Exit(1) }
+ if s[1] != 'e' {
+ os.Exit(1)
+ }
s = "good bye"
var p *string = &s
*p = "ciao"
return s
}
-
func main() {
s := sum([3]int{1, 2, 3}[:]) // a slice of the array is passed to sum
fmt.Print(s, "\n")