Skip to content

Commit 50e5516

Browse files
committed
add files
0 parents  commit 50e5516

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1837
-0
lines changed

array.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
var a [6]int
9+
fmt.Println("empty:", a)
10+
11+
a[3] = 100
12+
fmt.Println("set: ", a)
13+
fmt.Println("len: ", len(a))
14+
15+
//b := [5]int{1, 2, 3, 4, 5}
16+
var twoD [2][3]int
17+
for i := 0; i < 2; i++ {
18+
for j := 0; j < 3; j++ {
19+
twoD[i][j] = i + j
20+
}
21+
}
22+
fmt.Println("2d: ", twoD)
23+
}

atomic-counters.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"sync/atomic"
7+
"time"
8+
)
9+
10+
func main() {
11+
var ops uint64 = 0
12+
for i := 0; i < 50; i++ {
13+
go func() {
14+
for {
15+
atomic.AddUint64(&ops, 1)
16+
runtime.Gosched()
17+
}
18+
}()
19+
}
20+
time.Sleep(time.Second)
21+
22+
opsFinal := atomic.LoadUint64(&ops)
23+
fmt.Println("ops: ", opsFinal)
24+
}

base64-encoding.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
b64 "encoding/base64"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
data := "abc123!%^*&(*)_+)!@#"
10+
sEnc := b64.StdEncoding.EncodeToString([]byte(data))
11+
fmt.Println(sEnc)
12+
13+
sDec, _ := b64.StdEncoding.DecodeString(sEnc)
14+
fmt.Println(string(sDec))
15+
fmt.Println()
16+
// This encodes/decodes using a URL-compatible base64 format.
17+
uEnc := b64.URLEncoding.EncodeToString([]byte(data))
18+
fmt.Println(uEnc)
19+
uDec, _ := b64.URLEncoding.DecodeString(uEnc)
20+
fmt.Println(string(uDec))
21+
}

channel-buffering.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
messages := make(chan string, 2)
9+
10+
messages <- "buffered"
11+
fmt.Println(<-messages)
12+
messages <- "channel"
13+
messages <- "channel"
14+
15+
fmt.Println(<-messages)
16+
fmt.Println(<-messages)
17+
}

channel-directions.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// This ping function only accepts a channel for sending values. It would be a compile-time error to try to receive on this channel.
8+
func ping(pings chan<- string, msg string) {
9+
pings <- msg
10+
}
11+
12+
// The pong function accepts one channel for receives (pings) and a second for sends (pongs).
13+
func pong(pings <-chan string, pongs chan<- string) {
14+
msg := <-pings
15+
pongs <- msg
16+
}
17+
18+
func main() {
19+
pings := make(chan string, 1)
20+
pongs := make(chan string, 1)
21+
ping(pings, "passed message")
22+
pong(pings, pongs)
23+
fmt.Println(<-pongs)
24+
}

channel-synchronization.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func worker(done chan bool) {
9+
fmt.Println("working...")
10+
time.Sleep(time.Second)
11+
fmt.Println("done")
12+
done <- true
13+
}
14+
15+
func main() {
16+
done := make(chan bool, 1) // Start a worker goroutine, giving it the channel to notify on.
17+
18+
go worker(done)
19+
//Block until we receive a notification from the worker on the channel.
20+
<-done
21+
}
22+
23+
// This is the function we’ll run in a goroutine.
24+
// The done channel will be used to notify another goroutine that this
25+
// function’s work is done.
26+
// Send a value to notify that we’re done.
27+
28+
// If you removed the <- done line from this program,
29+
// the program would exit before the worker even started.

closing-channels.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func main() {
9+
jobs := make(chan int, 5)
10+
done := make(chan bool)
11+
go func() {
12+
for {
13+
j, more := <-jobs
14+
if more {
15+
fmt.Println("received job", j)
16+
} else {
17+
fmt.Println("received all jobs")
18+
done <- true // chan 要在不同的goroutine中
19+
return
20+
}
21+
}
22+
}()
23+
24+
for j := 1; j <= 3; j++ {
25+
jobs <- j
26+
fmt.Println("sent job", j)
27+
time.Sleep(time.Second * 1)
28+
}
29+
//This sends 3 jobs to the worker over the jobs channel, then closes it.
30+
close(jobs)
31+
fmt.Println("sent all jobs")
32+
<-done // chan 要在不同的goroutine中
33+
}

closures.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func intSeq(a int) func() int {
8+
i := 0
9+
return func() int {
10+
a += 1
11+
i += a + 1
12+
return i
13+
}
14+
}
15+
16+
func main() {
17+
n := intSeq(2)
18+
fmt.Println(n())
19+
fmt.Println(n()) //这一次会直接进入到闭包中执行,闭包外的 i:=0 这行代码不会被执行。闭包中的变量结果都会被保留并带入下一个闭包代码执行
20+
fmt.Println(n())
21+
fmt.Println(n())
22+
23+
m := intSeq(2)
24+
fmt.Println(m()) //另一个新的函数开始,和前面那个闭包毫无关系
25+
fmt.Println(n())
26+
}

collection-functions.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func Index(vs []string, t string) int {
9+
for i, v := range vs {
10+
if v == t {
11+
return i
12+
}
13+
}
14+
return -1
15+
}
16+
17+
func Include(vs []string, t string) bool {
18+
return Index(vs, t) >= 0
19+
}
20+
21+
func Any(vs []string, f func(string) bool) bool {
22+
for _, v := range vs {
23+
if f(v) {
24+
return true
25+
}
26+
}
27+
return false
28+
}
29+
30+
func All(vs []string, f func(string) bool) bool {
31+
for _, v := range vs {
32+
if !f(v) {
33+
return false
34+
}
35+
}
36+
return true
37+
}
38+
39+
func Filter(vs []string, f func(string) bool) []string {
40+
vsf := make([]string, 0)
41+
for _, v := range vs {
42+
if f(v) {
43+
vsf = append(vsf, v)
44+
}
45+
}
46+
return vsf
47+
}
48+
49+
func Map(vs []string, f func(string) string) []string {
50+
vsm := make([]string, len(vs))
51+
for i, v := range vs {
52+
vsm[i] = f(v)
53+
}
54+
return vsm
55+
}
56+
57+
func main() {
58+
var strs = []string{"peach", "apple", "pear", "plum"}
59+
fmt.Println(Index(strs, "pear"))
60+
fmt.Println(Include(strs, "grape"))
61+
fmt.Println(Any(strs, func(v string) bool {
62+
return strings.HasPrefix(v, "p")
63+
}))
64+
fmt.Println(All(strs, func(v string) bool {
65+
return strings.HasPrefix(v, "p")
66+
}))
67+
fmt.Println(Filter(strs, func(v string) bool {
68+
return strings.Contains(v, "e")
69+
}))
70+
71+
fmt.Println(Map(strs, strings.ToUpper))
72+
}

command-line-arguments.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "os"
4+
import "fmt"
5+
6+
func main() {
7+
// os.Args provides access to raw command-line arguments. Note that the first value in this slice is the path to the program, and os.Args[1:] holds the arguments to the program.
8+
argsWithProg := os.Args
9+
argsWithoutProg := os.Args[1:]
10+
// You can get individual args with normal indexing.
11+
arg := os.Args[3]
12+
fmt.Println(argsWithProg)
13+
fmt.Println(argsWithoutProg)
14+
fmt.Println(arg)
15+
}

command-line-flags.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
// Go provides a flag package supporting basic command-line flag parsing. We’ll use this package to implement our example command-line program.
4+
import "flag"
5+
import "fmt"
6+
7+
func main() {
8+
// Basic flag declarations are available for string, integer, and boolean options. Here we declare a string flag word with a default value "foo" and a short description. This flag.String function returns a string pointer (not a string value); we’ll see how to use this pointer below.
9+
wordPtr := flag.String("word", "foo", "a string")
10+
// This declares numb and fork flags, using a similar approach to the word flag.
11+
numbPtr := flag.Int("numb", 42, "an int")
12+
boolPtr := flag.Bool("fork", false, "a bool")
13+
// It’s also possible to declare an option that uses an existing var declared elsewhere in the program. Note that we need to pass in a pointer to the flag declaration function.
14+
var svar string
15+
flag.StringVar(&svar, "svar", "bar", "a string var")
16+
// Once all flags are declared, call flag.Parse() to execute the command-line parsing.
17+
flag.Parse()
18+
// Here we’ll just dump out the parsed options and any trailing positional arguments. Note that we need to dereference the pointers with e.g. *wordPtr to get the actual option values.
19+
fmt.Println("word:", *wordPtr)
20+
fmt.Println("numb:", *numbPtr)
21+
fmt.Println("fork:", *boolPtr)
22+
fmt.Println("svar:", svar)
23+
fmt.Println("tail:", flag.Args())
24+
}

defer.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
func main() {
9+
f := createFile("/tmp/defer.txt")
10+
defer closeFile(f)
11+
writeFile(f)
12+
}
13+
14+
func createFile(p string) *os.File {
15+
fmt.Println("Creating")
16+
f, err := os.Create(p)
17+
if err != nil {
18+
panic(err)
19+
}
20+
return f
21+
}
22+
23+
func writeFile(f *os.File) {
24+
fmt.Println("Writing")
25+
fmt.Fprintln(f, "data")
26+
}
27+
28+
func closeFile(f *os.File) {
29+
fmt.Println("Closing")
30+
f.Close()
31+
}

environment-variables.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
)
8+
9+
func main() {
10+
os.Setenv("FOO", "1")
11+
fmt.Println("FOO: ", os.Getenv("FOO"))
12+
fmt.Println("BAR: ", os.Getenv("BAR"))
13+
14+
fmt.Println()
15+
for _, e := range os.Environ() {
16+
pair := strings.Split(e, "=")
17+
fmt.Println(pair[0])
18+
}
19+
}

0 commit comments

Comments
 (0)