mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
doc/progs: gofmt -w
R=r, gri, r CC=golang-dev https://golang.org/cl/4662085
This commit is contained in:
parent
e86d727e60
commit
ab3365d34e
13 changed files with 57 additions and 60 deletions
|
|
@ -7,7 +7,7 @@ package main
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var u64 uint64 = 1<<64-1
|
var u64 uint64 = 1<<64 - 1
|
||||||
fmt.Printf("%d %d\n", u64, int64(u64))
|
fmt.Printf("%d %d\n", u64, int64(u64))
|
||||||
|
|
||||||
// harder stuff
|
// harder stuff
|
||||||
|
|
|
||||||
|
|
@ -42,8 +42,8 @@ func main() {
|
||||||
req.replyc = make(chan int)
|
req.replyc = make(chan int)
|
||||||
adder <- req
|
adder <- req
|
||||||
}
|
}
|
||||||
for i := N-1; i >= 0; i-- { // doesn't matter what order
|
for i := N - 1; i >= 0; i-- { // doesn't matter what order
|
||||||
if <-reqs[i].replyc != N + 2*i {
|
if <-reqs[i].replyc != N+2*i {
|
||||||
fmt.Println("fail at", i)
|
fmt.Println("fail at", i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,8 +47,8 @@ func main() {
|
||||||
req.replyc = make(chan int)
|
req.replyc = make(chan int)
|
||||||
adder <- req
|
adder <- req
|
||||||
}
|
}
|
||||||
for i := N-1; i >= 0; i-- { // doesn't matter what order
|
for i := N - 1; i >= 0; i-- { // doesn't matter what order
|
||||||
if <-reqs[i].replyc != N + 2*i {
|
if <-reqs[i].replyc != N+2*i {
|
||||||
fmt.Println("fail at", i)
|
fmt.Println("fail at", i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ func generate(ch chan int) {
|
||||||
func filter(in, out chan int, prime int) {
|
func filter(in, out chan int, prime int) {
|
||||||
for {
|
for {
|
||||||
i := <-in // Receive value of new variable 'i' from 'in'.
|
i := <-in // Receive value of new variable 'i' from 'in'.
|
||||||
if i % prime != 0 {
|
if i%prime != 0 {
|
||||||
out <- i // Send 'i' to channel 'out'.
|
out <- i // Send 'i' to channel 'out'.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import "fmt"
|
||||||
// Send the sequence 2, 3, 4, ... to returned channel
|
// Send the sequence 2, 3, 4, ... to returned channel
|
||||||
func generate() chan int {
|
func generate() chan int {
|
||||||
ch := make(chan int)
|
ch := make(chan int)
|
||||||
go func(){
|
go func() {
|
||||||
for i := 2; ; i++ {
|
for i := 2; ; i++ {
|
||||||
ch <- i
|
ch <- i
|
||||||
}
|
}
|
||||||
|
|
@ -22,7 +22,7 @@ func filter(in chan int, prime int) chan int {
|
||||||
out := make(chan int)
|
out := make(chan int)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
if i := <-in; i % prime != 0 {
|
if i := <-in; i%prime != 0 {
|
||||||
out <- i
|
out <- i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ func Sort(data Interface) {
|
||||||
func IsSorted(data Interface) bool {
|
func IsSorted(data Interface) bool {
|
||||||
n := data.Len()
|
n := data.Len()
|
||||||
for i := n - 1; i > 0; i-- {
|
for i := n - 1; i > 0; i-- {
|
||||||
if data.Less(i, i - 1) {
|
if data.Less(i, i-1) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -36,28 +36,24 @@ func (p IntSlice) Len() int { return len(p) }
|
||||||
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
|
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) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||||
|
|
||||||
|
|
||||||
type Float64Slice []float64
|
type Float64Slice []float64
|
||||||
|
|
||||||
func (p Float64Slice) Len() int { return len(p) }
|
func (p Float64Slice) Len() int { return len(p) }
|
||||||
func (p Float64Slice) Less(i, j int) bool { return p[i] < p[j] }
|
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) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||||
|
|
||||||
|
|
||||||
type StringSlice []string
|
type StringSlice []string
|
||||||
|
|
||||||
func (p StringSlice) Len() int { return len(p) }
|
func (p StringSlice) Len() int { return len(p) }
|
||||||
func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
|
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) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||||
|
|
||||||
|
|
||||||
// Convenience wrappers for common cases
|
// Convenience wrappers for common cases
|
||||||
|
|
||||||
func SortInts(a []int) { Sort(IntSlice(a)) }
|
func SortInts(a []int) { Sort(IntSlice(a)) }
|
||||||
func SortFloat64s(a []float64) { Sort(Float64Slice(a)) }
|
func SortFloat64s(a []float64) { Sort(Float64Slice(a)) }
|
||||||
func SortStrings(a []string) { Sort(StringSlice(a)) }
|
func SortStrings(a []string) { Sort(StringSlice(a)) }
|
||||||
|
|
||||||
|
|
||||||
func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) }
|
func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) }
|
||||||
func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) }
|
func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) }
|
||||||
func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) }
|
func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) }
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,9 @@ import "os"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := "hello"
|
s := "hello"
|
||||||
if s[1] != 'e' { os.Exit(1) }
|
if s[1] != 'e' {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
s = "good bye"
|
s = "good bye"
|
||||||
var p *string = &s
|
var p *string = &s
|
||||||
*p = "ciao"
|
*p = "ciao"
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ func sum(a []int) int { // returns an int
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := sum([3]int{1, 2, 3}[:]) // a slice of the array is passed to sum
|
s := sum([3]int{1, 2, 3}[:]) // a slice of the array is passed to sum
|
||||||
fmt.Print(s, "\n")
|
fmt.Print(s, "\n")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue