Pārlūkot izejas kodu

学习笔记备份提交

v1
SisMaker pirms 4 gadiem
vecāks
revīzija
3801555811
14 mainītis faili ar 349 papildinājumiem un 3 dzēšanām
  1. +2
    -0
      src/docs/go语言基础.md
  2. +24
    -0
      src/go.mod
  3. +35
    -0
      src/learn/eventWay/eventWay/eventWay.go
  4. +31
    -0
      src/learn/eventWay/main.go
  5. +20
    -3
      src/learn/interface/main.go
  6. +35
    -0
      src/learn/middleware.go
  7. +6
    -0
      src/learn/mulVar.go
  8. +34
    -0
      src/learn/mvc.go
  9. +44
    -0
      src/learn/mvc/mvc.go
  10. +23
    -0
      src/learn/mvc/mvcNoSignal.go
  11. +19
    -0
      src/learn/panic/panic.go
  12. +48
    -0
      src/learn/serv.go
  13. +9
    -0
      src/learn/tg.md
  14. +19
    -0
      src/testCase/mance.go

+ 2
- 0
src/docs/go语言基础.md Parādīt failu

@ -286,6 +286,8 @@
b = iota
c = iota
)
包级别的初始化 在main执行之前

+ 24
- 0
src/go.mod Parādīt failu

@ -1,3 +1,27 @@
module goUtils
go 1.12
require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/Joker/jade v1.0.0 // indirect
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398 // indirect
github.com/aymerick/raymond v2.0.2+incompatible // indirect
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/flosch/pongo2 v0.0.0-20200518135938-dfb43dbdc22a // indirect
github.com/gorilla/schema v1.1.0 // indirect
github.com/iris-contrib/blackfriday v2.0.0+incompatible // indirect
github.com/iris-contrib/formBinder v5.0.0+incompatible // indirect
github.com/iris-contrib/go.uuid v2.0.0+incompatible // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/kataras/golog v0.0.15 // indirect
github.com/kataras/iris v11.1.1+incompatible // indirect
github.com/klauspost/compress v1.10.6 // indirect
github.com/microcosm-cc/bluemonday v1.0.2 // indirect
github.com/ryanuber/columnize v2.1.0+incompatible // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a // indirect
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)

+ 35
- 0
src/learn/eventWay/eventWay/eventWay.go Parādīt failu

@ -0,0 +1,35 @@
package eventWay
type Event struct {
Name string
CfgId int
}
type HandlerErr struct {
Event Event
Handler Handler
ErrInfo error
}
type Handler func(msg interface{}) error
var eventMgr = map[Event][]Handler{}
func Register(event Event, handler Handler) {
if handlers, ok := eventMgr[event]; ok {
eventMgr[event] = append(handlers, handler)
} else {
eventMgr[event] = []Handler{handler}
}
}
func Publish(event Event, msg interface{}) (errs []HandlerErr) {
if handlers, ok := eventMgr[event]; ok {
for _, handler := range handlers {
if err := handler(msg); err != nil {
errs = append(errs, HandlerErr{Event: event, Handler: handler, ErrInfo: err})
}
}
}
return errs
}

+ 31
- 0
src/learn/eventWay/main.go Parādīt failu

@ -0,0 +1,31 @@
package main
import (
"fmt"
eventWay2 "goUtils/learn/eventWay/eventWay"
)
func hand1(msg interface{}) error {
println("IMY************ 111", msg)
return nil
}
func hand2(msg interface{}) error {
println("IMY************ 222", msg)
return nil
}
func hand3(msg interface{}) error {
fmt.Printf("IMY************ 333 %v\n", msg)
return nil
}
func main() {
eventWay2.Register(eventWay2.Event{Name: "str1"}, hand1)
eventWay2.Register(eventWay2.Event{Name: "str1"}, hand2)
eventWay2.Register(eventWay2.Event{Name: "str3"}, hand3)
eventWay2.Publish(eventWay2.Event{Name: "str1"}, "teste1")
eventWay2.Publish(eventWay2.Event{Name: "str3"}, "testt3")
eventWay2.Publish(eventWay2.Event{Name: "str2"}, "fdfdfdfdf")
}

+ 20
- 3
src/learn/interface/main.go Parādīt failu

@ -1,13 +1,24 @@
package main
import (
"fmt"
"goUtils/learn/interface/multiInter"
)
type T struct{}
type T struct {
aa int
bb string
}
type T2 struct {
aa2 int
bb2 string
}
func (t T) M1() { println("IMY******* M1", t.bb) }
func (T) M2() { println("IMY******* M2") }
func (T) M1() { println("IMY******* M1") }
func (T) M2() { println("IMY******* M2") }
func (T2) M2() { println("IMY******* M2") }
func f1(i multiInter.I1) { i.M1() }
func f2(i multiInter.I2) { i.M2() }
@ -15,4 +26,10 @@ func main() {
t := T{}
f1(t)
f2(t)
var i interface{} = T{11, "fdfdfdf"}
fmt.Printf("%v %[1]T\n", i.(T))
ii := i.(T)
f1(ii)
var t2 interface{} = T2{11, "aaaaaaaa"}
fmt.Printf("%v %[1]T\n", t2)
}

+ 35
- 0
src/learn/middleware.go Parādīt failu

@ -0,0 +1,35 @@
package main
import (
"github.com/kataras/iris"
)
func main() {
app := iris.New()
app.Get("/", before, mainHandler, after)
app.Run(iris.Addr(":8081"))
}
func before(ctx iris.Context) {
shareInfo := "this is a shareInfo between handlers"
requestPath := ctx.Path()
println("Before the mainHandler: " + requestPath)
ctx.Values().Set("info", shareInfo)
ctx.Next() //继续下一个handler
}
func after(ctx iris.Context) {
println("after the mainHandler")
}
func mainHandler(ctx iris.Context) {
println("Inside mainHandler")
info := ctx.Values().GetString("info")
ctx.HTML("<h1>Response</h1>")
ctx.HTML("<br/> Info: " + info)
ctx.Next()
}

+ 6
- 0
src/learn/mulVar.go Parādīt failu

@ -15,4 +15,10 @@ var e, f = 123, "hello"
func main() {
g, h := 123, "hello"
println(x, y, a, b, c, d, e, f, g, h)
var a, b, c int = 1, 2, 3
println(a, b, c)
var d float64 = 100
println(d)
}

+ 34
- 0
src/learn/mvc.go Parādīt failu

@ -0,0 +1,34 @@
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
"strconv"
)
func main() {
app := iris.New()
mvc.Configure(app.Party("/root"), myMVC)
app.Run(iris.Addr(":8082"))
}
func myMVC(app *mvc.Application) {
app.Handle(new(MyController))
}
type MyController struct{}
func (m *MyController) BeforeActivation(b mvc.BeforeActivation) {
b.Handle("GET", "/something/{id:long}", "MyCustomHandler")
}
// GET: http://localhost:8080/root
func (m *MyController) Get() string { return "Hey" }
// GET: http://localhost:8080/root/something/{id:long}
func (m *MyController) MyCustomHandler(id int64) string {
return "MyCustomHandler says Hey " + strconv.FormatInt(id, 10)
}

+ 44
- 0
src/learn/mvc/mvc.go Parādīt failu

@ -0,0 +1,44 @@
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
"github.com/kataras/iris/mvc"
)
func main() {
app := iris.New()
app.Use(recover.New())
app.Use(logger.New())
mvc.New(app).Handle(new(ExampleController))
// http://localhost:8080
// http://localhost:8080/ping
// http://localhost:8080/hello
app.Run(iris.Addr(":8080"))
}
type ExampleController struct{}
func (c *ExampleController) Get() mvc.Result {
return mvc.Response{
ContentType: "text/html",
Text: "<h1>Welcome</h1>",
}
}
func (c *ExampleController) GetPing() string {
return "Pong"
}
func (c *ExampleController) GetHello() interface{} {
return map[string]string{"massage": "hello Iris!"}
}
func (c *ExampleController) GetHellos() interface{} {
return map[string]string{"massage": "hellos Iris!"}
}

+ 23
- 0
src/learn/mvc/mvcNoSignal.go Parādīt failu

@ -0,0 +1,23 @@
package main
import (
"context"
"github.com/kataras/iris"
"time"
)
func main() {
app := iris.New()
iris.RegisterOnInterrupt(func() {
timeout := 5 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
// 关闭所有主机
println("IMY*********** todo stop")
app.Shutdown(ctx)
})
app.Get("/", func(ctx iris.Context) {
ctx.HTML("<h1>hi I just exist in order to see if the server is closed</h1>")
})
app.Run(iris.Addr(":8080"), iris.WithoutInterruptHandler)
}

+ 19
- 0
src/learn/panic/panic.go Parādīt failu

@ -0,0 +1,19 @@
package main
func test() {
var a int
defer func() {
if p := recover(); p != nil {
a = 1111
}
}()
panic(2222)
print(a)
}
func main() {
test()
}

+ 48
- 0
src/learn/serv.go Parādīt failu

@ -0,0 +1,48 @@
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
"github.com/valyala/tcplisten"
)
func main() {
app := iris.New()
app.Use(recover.New())
app.Use(logger.New())
// 输出html
// 请求方式:GET
// 访问地址:http://localhost:8080/welcome
app.Handle("GET", "/welcome", func(ctx iris.Context) {
ctx.HTML("<h1>Welcome</h1>")
})
// 输出字符串
// 类似于app.Handle("GET", "/ping", [...])
// 请求方式GET
// 请求地址:http://localhost:8080/ping
app.Get("/ping", func(ctx iris.Context) {
ctx.WriteString("Pong")
})
// 输出JSON
// 请求方式:GET
// 请求地址: http://localhost:8080/hello
app.Get("/hello", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "hello Iris!"})
})
listenerCfg := tcplisten.Config{
ReusePort: true,
DeferAccept: true,
FastOpen: true,
}
l, err := listenerCfg.NewListener("tcp4", ":8080")
if err != nil {
app.Logger().Fatal(err)
}
app.Run(iris.Listener(l)) //8080 监听端口
}

+ 9
- 0
src/learn/tg.md Parādīt failu

@ -0,0 +1,9 @@
SELECT DATE(FROM_UNIXTIME(created_at)) as group_date,COUNT(*) AS day1,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 1, 1, 0)) AS day2,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 2, 1, 0)) AS day3,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 3, 1, 0)) AS day4,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 4, 1, 0)) AS day5,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 5, 1, 0)) AS day6,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 6, 1, 0)) AS day7,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 14, 1, 0)) AS day14,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 30, 1, 0)) AS day30,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 60, 1, 0)) AS day60,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 90, 1, 0)) AS day90,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 120, 1, 0)) AS day120 FROM `roles` WHERE (created_at >= 1590451200 AND created_at <= 1590537599) AND (is_internal = false) AND (login_at != 0) GROUP BY DATE(FROM_UNIXTIME(created_at))
SELECT DATE(FROM_UNIXTIME(created_at)) as group_date,COUNT(*) AS day1,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 1, 1, 0)) AS day2,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 2, 1, 0)) AS day3,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 3, 1, 0)) AS day4,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 4, 1, 0)) AS day5,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 5, 1, 0)) AS day6,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 6, 1, 0)) AS day7,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 14, 1, 0)) AS day14,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 30, 1, 0)) AS day30,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 60, 1, 0)) AS day60,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 90, 1, 0)) AS day90,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 120, 1, 0)) AS day120 FROM `roles` WHERE (created_at >= 1590451200 AND created_at <= 1590537599) AND (is_internal = false) AND (login_at != 0) GROUP BY DATE(FROM_UNIXTIME(created_at))
SELECT DATE(FROM_UNIXTIME(created_at)) as group_date,COUNT(*) AS day1,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 1, 1, 0)) AS day2,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 2, 1, 0)) AS day3,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 3, 1, 0)) AS day4,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 4, 1, 0)) AS day5,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 5, 1, 0)) AS day6,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 6, 1, 0)) AS day7,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 14, 1, 0)) AS day14,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 30, 1, 0)) AS day30,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 60, 1, 0)) AS day60,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 90, 1, 0)) AS day90,SUM(if(DATEDIFF(FROM_UNIXTIME(login_at), FROM_UNIXTIME(created_at)) >= 120, 1, 0)) AS day120 FROM `roles` WHERE (created_at >= 1590451200 AND created_at <= 1590537599) AND (is_internal = false) AND (login_at != 0) GROUP BY DATE(FROM_UNIXTIME(created_at))

+ 19
- 0
src/testCase/mance.go Parādīt failu

@ -0,0 +1,19 @@
package main
import (
"os"
"strconv"
"time"
)
func test() {}
func main() {
start := time.Now()
cnt, _ := strconv.Atoi(os.Args[1])
for i := 0; i < cnt; i++ {
test()
}
println("IMY*****main*", time.Since(start).Microseconds(), "us")
}

Notiek ielāde…
Atcelt
Saglabāt