Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
ab3365d3
Commit
ab3365d3
authored
Jul 09, 2011
by
Mikio Hara
Committed by
Rob Pike
Jul 09, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
doc/progs: gofmt -w
R=r, gri, r CC=golang-dev
https://golang.org/cl/4662085
parent
e86d727e
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
57 additions
and
60 deletions
+57
-60
echo.go
doc/progs/echo.go
+3
-3
file_windows.go
doc/progs/file_windows.go
+2
-2
helloworld.go
doc/progs/helloworld.go
+1
-1
helloworld3.go
doc/progs/helloworld3.go
+1
-1
print.go
doc/progs/print.go
+1
-1
server.go
doc/progs/server.go
+5
-5
server1.go
doc/progs/server1.go
+5
-5
sieve.go
doc/progs/sieve.go
+6
-6
sieve1.go
doc/progs/sieve1.go
+2
-2
sort.go
doc/progs/sort.go
+16
-20
sortmain.go
doc/progs/sortmain.go
+12
-12
strings.go
doc/progs/strings.go
+3
-1
sum.go
doc/progs/sum.go
+0
-1
No files found.
doc/progs/echo.go
View file @
ab3365d3
...
@@ -6,18 +6,18 @@ package main
...
@@ -6,18 +6,18 @@ package main
import
(
import
(
"os"
"os"
"flag"
// command line option parser
"flag"
// command line option parser
)
)
var
omitNewline
=
flag
.
Bool
(
"n"
,
false
,
"don't print final newline"
)
var
omitNewline
=
flag
.
Bool
(
"n"
,
false
,
"don't print final newline"
)
const
(
const
(
Space
=
" "
Space
=
" "
Newline
=
"
\n
"
Newline
=
"
\n
"
)
)
func
main
()
{
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
=
""
var
s
string
=
""
for
i
:=
0
;
i
<
flag
.
NArg
();
i
++
{
for
i
:=
0
;
i
<
flag
.
NArg
();
i
++
{
if
i
>
0
{
if
i
>
0
{
...
...
doc/progs/file_windows.go
View file @
ab3365d3
...
@@ -10,8 +10,8 @@ import (
...
@@ -10,8 +10,8 @@ import (
)
)
type
File
struct
{
type
File
struct
{
fd
syscall
.
Handle
// file descriptor number
fd
syscall
.
Handle
// file descriptor number
name
string
// file name at Open time
name
string
// file name at Open time
}
}
func
newFile
(
fd
syscall
.
Handle
,
name
string
)
*
File
{
func
newFile
(
fd
syscall
.
Handle
,
name
string
)
*
File
{
...
...
doc/progs/helloworld.go
View file @
ab3365d3
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
package
main
package
main
import
fmt
"fmt"
// Package implementing formatted I/O.
import
fmt
"fmt"
// Package implementing formatted I/O.
func
main
()
{
func
main
()
{
fmt
.
Printf
(
"Hello, world; or Καλημέρα κόσμε; or こんにちは 世界
\n
"
)
fmt
.
Printf
(
"Hello, world; or Καλημέρα κόσμε; or こんにちは 世界
\n
"
)
...
...
doc/progs/helloworld3.go
View file @
ab3365d3
...
@@ -15,7 +15,7 @@ func main() {
...
@@ -15,7 +15,7 @@ func main() {
file
.
Stdout
.
Write
(
hello
)
file
.
Stdout
.
Write
(
hello
)
f
,
err
:=
file
.
Open
(
"/does/not/exist"
)
f
,
err
:=
file
.
Open
(
"/does/not/exist"
)
if
f
==
nil
{
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
)
os
.
Exit
(
1
)
}
}
}
}
doc/progs/print.go
View file @
ab3365d3
...
@@ -7,7 +7,7 @@ package main
...
@@ -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
...
...
doc/progs/server.go
View file @
ab3365d3
...
@@ -7,8 +7,8 @@ package main
...
@@ -7,8 +7,8 @@ package main
import
"fmt"
import
"fmt"
type
request
struct
{
type
request
struct
{
a
,
b
int
a
,
b
int
replyc
chan
int
replyc
chan
int
}
}
type
binOp
func
(
a
,
b
int
)
int
type
binOp
func
(
a
,
b
int
)
int
...
@@ -21,7 +21,7 @@ func run(op binOp, req *request) {
...
@@ -21,7 +21,7 @@ func run(op binOp, req *request) {
func
server
(
op
binOp
,
service
chan
*
request
)
{
func
server
(
op
binOp
,
service
chan
*
request
)
{
for
{
for
{
req
:=
<-
service
req
:=
<-
service
go
run
(
op
,
req
)
// don't wait for it
go
run
(
op
,
req
)
// don't wait for it
}
}
}
}
...
@@ -42,8 +42,8 @@ func main() {
...
@@ -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
)
}
}
}
}
...
...
doc/progs/server1.go
View file @
ab3365d3
...
@@ -7,8 +7,8 @@ package main
...
@@ -7,8 +7,8 @@ package main
import
"fmt"
import
"fmt"
type
request
struct
{
type
request
struct
{
a
,
b
int
a
,
b
int
replyc
chan
int
replyc
chan
int
}
}
type
binOp
func
(
a
,
b
int
)
int
type
binOp
func
(
a
,
b
int
)
int
...
@@ -22,7 +22,7 @@ func server(op binOp, service chan *request, quit chan bool) {
...
@@ -22,7 +22,7 @@ func server(op binOp, service chan *request, quit chan bool) {
for
{
for
{
select
{
select
{
case
req
:=
<-
service
:
case
req
:=
<-
service
:
go
run
(
op
,
req
)
// don't wait for it
go
run
(
op
,
req
)
// don't wait for it
case
<-
quit
:
case
<-
quit
:
return
return
}
}
...
@@ -47,8 +47,8 @@ func main() {
...
@@ -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
)
}
}
}
}
...
...
doc/progs/sieve.go
View file @
ab3365d3
...
@@ -9,7 +9,7 @@ import "fmt"
...
@@ -9,7 +9,7 @@ import "fmt"
// Send the sequence 2, 3, 4, ... to channel 'ch'.
// Send the sequence 2, 3, 4, ... to channel 'ch'.
func
generate
(
ch
chan
int
)
{
func
generate
(
ch
chan
int
)
{
for
i
:=
2
;
;
i
++
{
for
i
:=
2
;
;
i
++
{
ch
<-
i
// Send 'i' to channel 'ch'.
ch
<-
i
// Send 'i' to channel 'ch'.
}
}
}
}
...
@@ -17,17 +17,17 @@ func generate(ch chan int) {
...
@@ -17,17 +17,17 @@ func generate(ch chan int) {
// removing those divisible by 'prime'.
// removing those divisible by 'prime'.
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'.
}
}
}
}
}
}
// The prime sieve: Daisy-chain filter processes together.
// The prime sieve: Daisy-chain filter processes together.
func
main
()
{
func
main
()
{
ch
:=
make
(
chan
int
)
// Create a new channel.
ch
:=
make
(
chan
int
)
// Create a new channel.
go
generate
(
ch
)
// Start generate() as a goroutine.
go
generate
(
ch
)
// Start generate() as a goroutine.
for
i
:=
0
;
i
<
100
;
i
++
{
// Print the first hundred primes.
for
i
:=
0
;
i
<
100
;
i
++
{
// Print the first hundred primes.
prime
:=
<-
ch
prime
:=
<-
ch
fmt
.
Println
(
prime
)
fmt
.
Println
(
prime
)
...
...
doc/progs/sieve1.go
View file @
ab3365d3
...
@@ -9,7 +9,7 @@ import "fmt"
...
@@ -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 {
...
@@ -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
}
}
}
}
...
...
doc/progs/sort.go
View file @
ab3365d3
...
@@ -21,7 +21,7 @@ func Sort(data Interface) {
...
@@ -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
}
}
}
}
...
@@ -32,32 +32,28 @@ func IsSorted(data Interface) bool {
...
@@ -32,32 +32,28 @@ func IsSorted(data Interface) bool {
type
IntSlice
[]
int
type
IntSlice
[]
int
func
(
p
IntSlice
)
Len
()
int
{
return
len
(
p
)
}
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
))
}
doc/progs/sortmain.go
View file @
ab3365d3
...
@@ -28,27 +28,27 @@ func strings() {
...
@@ -28,27 +28,27 @@ func strings() {
}
}
type
day
struct
{
type
day
struct
{
num
int
num
int
shortName
string
shortName
string
longName
string
longName
string
}
}
type
dayArray
struct
{
type
dayArray
struct
{
data
[]
*
day
data
[]
*
day
}
}
func
(
p
*
dayArray
)
Len
()
int
{
return
len
(
p
.
data
)
}
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
)
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
)
Swap
(
i
,
j
int
)
{
p
.
data
[
i
],
p
.
data
[
j
]
=
p
.
data
[
j
],
p
.
data
[
i
]
}
func
days
()
{
func
days
()
{
Sunday
:=
day
{
0
,
"SUN"
,
"Sunday"
}
Sunday
:=
day
{
0
,
"SUN"
,
"Sunday"
}
Monday
:=
day
{
1
,
"MON"
,
"Monday"
}
Monday
:=
day
{
1
,
"MON"
,
"Monday"
}
Tuesday
:=
day
{
2
,
"TUE"
,
"Tuesday"
}
Tuesday
:=
day
{
2
,
"TUE"
,
"Tuesday"
}
Wednesday
:=
day
{
3
,
"WED"
,
"Wednesday"
}
Wednesday
:=
day
{
3
,
"WED"
,
"Wednesday"
}
Thursday
:=
day
{
4
,
"THU"
,
"Thursday"
}
Thursday
:=
day
{
4
,
"THU"
,
"Thursday"
}
Friday
:=
day
{
5
,
"FRI"
,
"Friday"
}
Friday
:=
day
{
5
,
"FRI"
,
"Friday"
}
Saturday
:=
day
{
6
,
"SAT"
,
"Saturday"
}
Saturday
:=
day
{
6
,
"SAT"
,
"Saturday"
}
data
:=
[]
*
day
{
&
Tuesday
,
&
Thursday
,
&
Wednesday
,
&
Sunday
,
&
Monday
,
&
Friday
,
&
Saturday
}
data
:=
[]
*
day
{
&
Tuesday
,
&
Thursday
,
&
Wednesday
,
&
Sunday
,
&
Monday
,
&
Friday
,
&
Saturday
}
a
:=
dayArray
{
data
}
a
:=
dayArray
{
data
}
sort
.
Sort
(
&
a
)
sort
.
Sort
(
&
a
)
...
...
doc/progs/strings.go
View file @
ab3365d3
...
@@ -8,7 +8,9 @@ import "os"
...
@@ -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"
...
...
doc/progs/sum.go
View file @
ab3365d3
...
@@ -14,7 +14,6 @@ func sum(a []int) int { // returns an int
...
@@ -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
"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment