Skip to content

Commit

Permalink
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
Show file tree
Hide file tree
Showing 25 changed files with 1,588 additions and 48 deletions.
13 changes: 11 additions & 2 deletions go.mod
Expand Up @@ -4,8 +4,11 @@ go 1.13

require (
github.com/astaxie/beego v1.12.1
github.com/coreos/etcd v3.3.13+incompatible
github.com/gin-gonic/gin v1.6.2
github.com/go-redis/redis/v8 v8.0.0-beta.6
github.com/go-sql-driver/mysql v1.5.0
github.com/gophercloud/gophercloud v0.11.0 // indirect
github.com/imdario/mergo v0.3.9 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/olivere/elastic/v7 v7.0.15
Expand All @@ -16,14 +19,20 @@ require (
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.6.3
github.com/stretchr/testify v1.5.1
github.com/stretchr/testify v1.6.1
github.com/urfave/cli v1.22.4
github.com/urfave/negroni v1.0.0
go.uber.org/zap v1.15.0
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
google.golang.org/grpc/examples v0.0.0-20200721210703-a5a36bd3f0bb // indirect
gopkg.in/yaml.v2 v2.2.8
helm.sh/helm/v3 v3.2.4
k8s.io/api v0.18.2
k8s.io/apimachinery v0.18.2
k8s.io/client-go v11.0.0+incompatible
k8s.io/client-go v0.18.2
k8s.io/klog v1.0.0
k8s.io/utils v0.0.0-20200414100711-2df71ebbae66 // indirect
rsc.io/letsencrypt v0.0.3 // indirect
)

replace google.golang.org/grpc => github.com/grpc/grpc-go v1.29.0
601 changes: 601 additions & 0 deletions go.sum

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions httpserver/mux/mux.go
@@ -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 added httpserver/play
Binary file not shown.
39 changes: 39 additions & 0 deletions httpserver/play.go
@@ -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)
}
47 changes: 47 additions & 0 deletions itermap/itermap.go
@@ -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 added leaderelection/leader
Binary file not shown.
68 changes: 68 additions & 0 deletions leaderelection/leaderelection.go
@@ -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)
}
}
22 changes: 22 additions & 0 deletions pkgvar/cmd/main.go
@@ -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()
}
11 changes: 11 additions & 0 deletions pkgvar/pkga/types.go
@@ -0,0 +1,11 @@
package pkga

var V1 string = "in pkg 1"

type MyStruct struct {
Name string
}

func NewStruct() MyStruct {
return MyStruct{V1}
}
17 changes: 17 additions & 0 deletions pkgvar/pkgb/types.go
@@ -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)
}
21 changes: 21 additions & 0 deletions pkgvar/pkgc/types.go
@@ -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)
}
11 changes: 11 additions & 0 deletions play/play.go
@@ -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))
}
}
31 changes: 10 additions & 21 deletions reg/reg.go
Expand Up @@ -5,24 +5,13 @@ import (
"regexp"
)

var kubeVersionRegex = regexp.MustCompile("^v([\\d]+)(?:(alpha|beta)([\\d]+))?$")

func main(){
//pattern := regexp.MustCompile("(?:my)")
//str := "myname.youname"
//result := pattern.FindAllString(str, 0)
//fmt.Printf("%#s", result)
//looptest()
v := "v2"
fmt.Println(kubeVersionRegex.FindStringSubmatch(v))
fmt.Println(int(""))
}

func looptest() {
lastPort := 10000
for offset := 1; offset < 100; offset++ {
port := lastPort + offset
//for port := lastPort + 1; port < 100; port++ {
fmt.Println(port)
}
}
func main() {
var r1 = regexp.MustCompile(`(((\d+)-(\d+))-((\d+)-(\d+)))`)
var r2 = regexp.MustCompile(`(?:\d+)-(\d+)-(\d+)-(\d+)`)
var r3 = regexp.MustCompile(`(?P<first>\d+)-(\d+)-(\d+)-(\d+)`)
var s string = "abc1-2-3-4-5-6-7"
fmt.Println(r1.FindStringSubmatch(s))
fmt.Println(r2.FindStringSubmatch(s))
fmt.Println(r3.FindStringSubmatch(s))
fmt.Println(r3.SubexpNames()[5])
}
14 changes: 14 additions & 0 deletions string/main.go
@@ -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 added trycontext/trycontext
Binary file not shown.

0 comments on commit 6c3684f

Please sign in to comment.