Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
提交最近的练习代码
- Loading branch information
Guangming Wang
committed
Aug 6, 2020
1 parent
bea4d0b
commit 6c3684f
Showing
25 changed files
with
1,588 additions
and
48 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
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,30 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/api/", apiHandler{}) | ||
mux.HandleFunc("/", func (w http.ResponseWriter, r *http.Request){ | ||
if r.URL.Path != "/" { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
fmt.Fprint(w, "welcome to index") | ||
}) | ||
var srv = &http.Server{ | ||
Addr: ":8080", | ||
Handler: mux, | ||
} | ||
log.Fatal(srv.ListenAndServe()) | ||
} | ||
|
||
type apiHandler struct{} | ||
|
||
func (a apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, "api") | ||
} |
Binary file not shown.
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,39 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type query func(string) string | ||
|
||
func exec(name string, vs ...query) string { | ||
ch := make(chan string) | ||
fn := func(i int) { | ||
ch <- vs[i](name) | ||
} | ||
var a string | ||
for i, _ := range vs { | ||
k := i | ||
go fn(k) | ||
// 异步变同步 | ||
s := <- ch | ||
a += s | ||
} | ||
// 这里只会从chan中接收一个值,后面的会一直阻塞 | ||
//此时的返回值是随机的 | ||
//return <-ch | ||
return a | ||
} | ||
|
||
func main() { | ||
ret := exec("111", func(n string) string { | ||
return n + "func1" | ||
}, func(n string) string { | ||
return n + "func2" | ||
}, func(n string) string { | ||
return n + "func3" | ||
}, func(n string) string { | ||
return n + "func4" | ||
}) | ||
fmt.Println(ret) | ||
} |
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,47 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
var m map[string]interface{} | ||
var n map[string]string | ||
var o map[int]string | ||
var p map[string]int | ||
var q map[string]interface{} | ||
|
||
n = map[string]string{ | ||
"n1": "s1", | ||
"n2": "s2", | ||
} | ||
o = map[int]string{ | ||
1: "o1", | ||
2: "o2", | ||
} | ||
p = map[string]int{ | ||
"p1": 1, | ||
"p2": 2, | ||
} | ||
q = map[string]interface{}{ | ||
"q1": "i1", | ||
"q2": "i2", | ||
} | ||
m = map[string]interface{}{ | ||
"m1": "v1", | ||
"m2": n, | ||
"m3": o, | ||
"m4": p, | ||
"m5": q, | ||
} | ||
iterMap("", m) | ||
} | ||
|
||
func iterMap(prefix string, m map[string]interface{}) { | ||
for k, v := range m { | ||
switch rv := v.(type) { | ||
case map[string]interface{}: | ||
iterMap(k, rv) | ||
default: | ||
fmt.Printf("%v.%v: %v\n", prefix, k, v) | ||
} | ||
} | ||
} |
Binary file not shown.
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,68 @@ | ||
package main | ||
// https://medium.com/@felipedutratine/leader-election-in-go-with-etcd-2ca8f3876d79 | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"github.com/coreos/etcd/clientv3" | ||
"github.com/coreos/etcd/clientv3/concurrency" | ||
"log" | ||
"time" | ||
) | ||
// https://kubernetes.io/blog/2019/08/30/announcing-etcd-3-4/ | ||
// https://etcd.io/docs/v3.4.0/dev-guide/interacting_v3/ | ||
// https://etcd.io/docs/v3.4.0/op-guide/container/ | ||
// docker run --rm -e ALLOW_NONE_AUTHENTICATION=yes -p2379:2379 -p2380:2380 bitnami/etcd:3.4.9 | ||
func main() { | ||
var name = flag.String("name", "", "instance name for this process") | ||
flag.Parse() | ||
|
||
cli, err := clientv3.New(clientv3.Config{ | ||
Endpoints: []string{"localhost:2379"}, | ||
}) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer cli.Close() | ||
|
||
s, err := concurrency.NewSession(cli) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer s.Close() | ||
|
||
e := concurrency.NewElection(s, "/test-group-leader/") | ||
ctx := context.Background() | ||
// 定时查询leader是谁 | ||
go func(ctx context.Context, e *concurrency.Election) { | ||
for { | ||
resp, err := e.Leader(ctx) | ||
if err != nil { | ||
if err != concurrency.ErrElectionNoLeader { | ||
log.Println("leader returned error", err) | ||
return | ||
} | ||
time.Sleep(2*time.Second) | ||
continue | ||
} | ||
log.Println("current leader is ", string(resp.Kvs[0].Value)) | ||
time.Sleep(2*time.Second) | ||
continue | ||
} | ||
}(ctx, e) | ||
// 执行选主循环 | ||
for { | ||
if err := e.Campaign(ctx, *name); err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Println("win leader election for ", *name) | ||
log.Println("Do some work as leader in ", *name) | ||
time.Sleep(5*time.Second) | ||
// 如果leader不主动退位且实例已经挂掉的话,会有问题,导致无主 | ||
if err := e.Resign(ctx); err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Println("leader resign ", *name) | ||
} | ||
} |
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,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/beautytiger/go-playground/pkgvar/pkga" | ||
"github.com/beautytiger/go-playground/pkgvar/pkgb" | ||
"github.com/beautytiger/go-playground/pkgvar/pkgc" | ||
) | ||
|
||
func main() { | ||
fmt.Println(pkga.V1) | ||
pkga.V1 = "in main" | ||
//fmt.Println(pkgb.V2) | ||
s := pkga.NewStruct() | ||
fmt.Println(s.Name) | ||
|
||
//c :=pkgc.NewC() | ||
//fmt.Println(c.Name) | ||
|
||
pkgc.Run() | ||
pkgb.Run() | ||
} |
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,11 @@ | ||
package pkga | ||
|
||
var V1 string = "in pkg 1" | ||
|
||
type MyStruct struct { | ||
Name string | ||
} | ||
|
||
func NewStruct() MyStruct { | ||
return MyStruct{V1} | ||
} |
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 pkgb | ||
|
||
import ( | ||
"fmt" | ||
"github.com/beautytiger/go-playground/pkgvar/pkga" | ||
) | ||
|
||
var V2 string = "in pkg 2" | ||
|
||
|
||
func init() { | ||
pkga.V1 = "changed in pkg b" | ||
} | ||
|
||
func Run() { | ||
fmt.Println("in package b", pkga.V1) | ||
} |
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 pkgc | ||
|
||
import ( | ||
"fmt" | ||
"github.com/beautytiger/go-playground/pkgvar/pkga" | ||
) | ||
|
||
var V3 string = "in package c" | ||
|
||
type C struct { | ||
Name string | ||
} | ||
|
||
func NewC() C { | ||
pkga.V1 = "changed in pkg c" | ||
return C{pkga.V1} | ||
} | ||
|
||
func Run() { | ||
fmt.Println("work in pkg c", pkga.V1) | ||
} |
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,11 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
a := make([]int, 5) | ||
for i := 1; i < 20; i++ { | ||
a = append(a, i) | ||
fmt.Printf("%d: slice len %d, cap %d\n", i, len(a), cap(a)) | ||
} | ||
} |
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
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,14 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"unicode/utf8" | ||
) | ||
|
||
func main() { | ||
x := "my new text is this long" | ||
y := strings.Repeat("#", utf8.RuneCountInString(x)) | ||
fmt.Println(x) | ||
fmt.Println(y) | ||
} |
Binary file not shown.
Oops, something went wrong.