-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 50e5516
Showing
58 changed files
with
1,837 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
var a [6]int | ||
fmt.Println("empty:", a) | ||
|
||
a[3] = 100 | ||
fmt.Println("set: ", a) | ||
fmt.Println("len: ", len(a)) | ||
|
||
//b := [5]int{1, 2, 3, 4, 5} | ||
var twoD [2][3]int | ||
for i := 0; i < 2; i++ { | ||
for j := 0; j < 3; j++ { | ||
twoD[i][j] = i + j | ||
} | ||
} | ||
fmt.Println("2d: ", twoD) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
func main() { | ||
var ops uint64 = 0 | ||
for i := 0; i < 50; i++ { | ||
go func() { | ||
for { | ||
atomic.AddUint64(&ops, 1) | ||
runtime.Gosched() | ||
} | ||
}() | ||
} | ||
time.Sleep(time.Second) | ||
|
||
opsFinal := atomic.LoadUint64(&ops) | ||
fmt.Println("ops: ", opsFinal) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
b64 "encoding/base64" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
data := "abc123!%^*&(*)_+)!@#" | ||
sEnc := b64.StdEncoding.EncodeToString([]byte(data)) | ||
fmt.Println(sEnc) | ||
|
||
sDec, _ := b64.StdEncoding.DecodeString(sEnc) | ||
fmt.Println(string(sDec)) | ||
fmt.Println() | ||
// This encodes/decodes using a URL-compatible base64 format. | ||
uEnc := b64.URLEncoding.EncodeToString([]byte(data)) | ||
fmt.Println(uEnc) | ||
uDec, _ := b64.URLEncoding.DecodeString(uEnc) | ||
fmt.Println(string(uDec)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
messages := make(chan string, 2) | ||
|
||
messages <- "buffered" | ||
fmt.Println(<-messages) | ||
messages <- "channel" | ||
messages <- "channel" | ||
|
||
fmt.Println(<-messages) | ||
fmt.Println(<-messages) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// This ping function only accepts a channel for sending values. It would be a compile-time error to try to receive on this channel. | ||
func ping(pings chan<- string, msg string) { | ||
pings <- msg | ||
} | ||
|
||
// The pong function accepts one channel for receives (pings) and a second for sends (pongs). | ||
func pong(pings <-chan string, pongs chan<- string) { | ||
msg := <-pings | ||
pongs <- msg | ||
} | ||
|
||
func main() { | ||
pings := make(chan string, 1) | ||
pongs := make(chan string, 1) | ||
ping(pings, "passed message") | ||
pong(pings, pongs) | ||
fmt.Println(<-pongs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func worker(done chan bool) { | ||
fmt.Println("working...") | ||
time.Sleep(time.Second) | ||
fmt.Println("done") | ||
done <- true | ||
} | ||
|
||
func main() { | ||
done := make(chan bool, 1) // Start a worker goroutine, giving it the channel to notify on. | ||
|
||
go worker(done) | ||
//Block until we receive a notification from the worker on the channel. | ||
<-done | ||
} | ||
|
||
// This is the function we’ll run in a goroutine. | ||
// The done channel will be used to notify another goroutine that this | ||
// function’s work is done. | ||
// Send a value to notify that we’re done. | ||
|
||
// If you removed the <- done line from this program, | ||
// the program would exit before the worker even started. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func main() { | ||
jobs := make(chan int, 5) | ||
done := make(chan bool) | ||
go func() { | ||
for { | ||
j, more := <-jobs | ||
if more { | ||
fmt.Println("received job", j) | ||
} else { | ||
fmt.Println("received all jobs") | ||
done <- true // chan 要在不同的goroutine中 | ||
return | ||
} | ||
} | ||
}() | ||
|
||
for j := 1; j <= 3; j++ { | ||
jobs <- j | ||
fmt.Println("sent job", j) | ||
time.Sleep(time.Second * 1) | ||
} | ||
//This sends 3 jobs to the worker over the jobs channel, then closes it. | ||
close(jobs) | ||
fmt.Println("sent all jobs") | ||
<-done // chan 要在不同的goroutine中 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func intSeq(a int) func() int { | ||
i := 0 | ||
return func() int { | ||
a += 1 | ||
i += a + 1 | ||
return i | ||
} | ||
} | ||
|
||
func main() { | ||
n := intSeq(2) | ||
fmt.Println(n()) | ||
fmt.Println(n()) //这一次会直接进入到闭包中执行,闭包外的 i:=0 这行代码不会被执行。闭包中的变量结果都会被保留并带入下一个闭包代码执行 | ||
fmt.Println(n()) | ||
fmt.Println(n()) | ||
|
||
m := intSeq(2) | ||
fmt.Println(m()) //另一个新的函数开始,和前面那个闭包毫无关系 | ||
fmt.Println(n()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func Index(vs []string, t string) int { | ||
for i, v := range vs { | ||
if v == t { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func Include(vs []string, t string) bool { | ||
return Index(vs, t) >= 0 | ||
} | ||
|
||
func Any(vs []string, f func(string) bool) bool { | ||
for _, v := range vs { | ||
if f(v) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func All(vs []string, f func(string) bool) bool { | ||
for _, v := range vs { | ||
if !f(v) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func Filter(vs []string, f func(string) bool) []string { | ||
vsf := make([]string, 0) | ||
for _, v := range vs { | ||
if f(v) { | ||
vsf = append(vsf, v) | ||
} | ||
} | ||
return vsf | ||
} | ||
|
||
func Map(vs []string, f func(string) string) []string { | ||
vsm := make([]string, len(vs)) | ||
for i, v := range vs { | ||
vsm[i] = f(v) | ||
} | ||
return vsm | ||
} | ||
|
||
func main() { | ||
var strs = []string{"peach", "apple", "pear", "plum"} | ||
fmt.Println(Index(strs, "pear")) | ||
fmt.Println(Include(strs, "grape")) | ||
fmt.Println(Any(strs, func(v string) bool { | ||
return strings.HasPrefix(v, "p") | ||
})) | ||
fmt.Println(All(strs, func(v string) bool { | ||
return strings.HasPrefix(v, "p") | ||
})) | ||
fmt.Println(Filter(strs, func(v string) bool { | ||
return strings.Contains(v, "e") | ||
})) | ||
|
||
fmt.Println(Map(strs, strings.ToUpper)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import "os" | ||
import "fmt" | ||
|
||
func main() { | ||
// 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. | ||
argsWithProg := os.Args | ||
argsWithoutProg := os.Args[1:] | ||
// You can get individual args with normal indexing. | ||
arg := os.Args[3] | ||
fmt.Println(argsWithProg) | ||
fmt.Println(argsWithoutProg) | ||
fmt.Println(arg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
// Go provides a flag package supporting basic command-line flag parsing. We’ll use this package to implement our example command-line program. | ||
import "flag" | ||
import "fmt" | ||
|
||
func main() { | ||
// 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. | ||
wordPtr := flag.String("word", "foo", "a string") | ||
// This declares numb and fork flags, using a similar approach to the word flag. | ||
numbPtr := flag.Int("numb", 42, "an int") | ||
boolPtr := flag.Bool("fork", false, "a bool") | ||
// 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. | ||
var svar string | ||
flag.StringVar(&svar, "svar", "bar", "a string var") | ||
// Once all flags are declared, call flag.Parse() to execute the command-line parsing. | ||
flag.Parse() | ||
// 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. | ||
fmt.Println("word:", *wordPtr) | ||
fmt.Println("numb:", *numbPtr) | ||
fmt.Println("fork:", *boolPtr) | ||
fmt.Println("svar:", svar) | ||
fmt.Println("tail:", flag.Args()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func main() { | ||
f := createFile("/tmp/defer.txt") | ||
defer closeFile(f) | ||
writeFile(f) | ||
} | ||
|
||
func createFile(p string) *os.File { | ||
fmt.Println("Creating") | ||
f, err := os.Create(p) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return f | ||
} | ||
|
||
func writeFile(f *os.File) { | ||
fmt.Println("Writing") | ||
fmt.Fprintln(f, "data") | ||
} | ||
|
||
func closeFile(f *os.File) { | ||
fmt.Println("Closing") | ||
f.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
os.Setenv("FOO", "1") | ||
fmt.Println("FOO: ", os.Getenv("FOO")) | ||
fmt.Println("BAR: ", os.Getenv("BAR")) | ||
|
||
fmt.Println() | ||
for _, e := range os.Environ() { | ||
pair := strings.Split(e, "=") | ||
fmt.Println(pair[0]) | ||
} | ||
} |
Oops, something went wrong.