From 22a36606d62c4c0e6a9a929dbde2a834e1026e36 Mon Sep 17 00:00:00 2001 From: shfshanyue Date: Sun, 27 Mar 2022 15:22:10 +0800 Subject: [PATCH] feat: go --- go/hello/defer.go | 23 +++++++++++++++++++++ go/hello/go.mod | 3 +++ go/hello/main.go | 38 +++++++++++++++++++++++++++++++++++ go/hello/routine.go | 13 ++++++++++++ node-native/encoding/index.js | 10 +++++++++ node-server/koa/index.js | 14 +++++++++++++ 6 files changed, 101 insertions(+) create mode 100644 go/hello/defer.go create mode 100644 go/hello/go.mod create mode 100644 go/hello/main.go create mode 100644 go/hello/routine.go diff --git a/go/hello/defer.go b/go/hello/defer.go new file mode 100644 index 0000000..a55e314 --- /dev/null +++ b/go/hello/defer.go @@ -0,0 +1,23 @@ +package main + +import "fmt" + +func test() int { + i := 0 + defer func() { + fmt.Println("defer1") + }() + defer func() { + i += 1 + fmt.Println("defer2") + }() + return i +} + +func main() { + // Output: + // defer2 + // defer1 + // return 0 + fmt.Println("return", test()) +} diff --git a/go/hello/go.mod b/go/hello/go.mod new file mode 100644 index 0000000..afdf576 --- /dev/null +++ b/go/hello/go.mod @@ -0,0 +1,3 @@ +module hello + +go 1.17 diff --git a/go/hello/main.go b/go/hello/main.go new file mode 100644 index 0000000..b53b4f2 --- /dev/null +++ b/go/hello/main.go @@ -0,0 +1,38 @@ +package main + +import "fmt" + +const ( + RED = iota + GREEN + BLUE +) + +func main() { + // 赋值 + var a int = 10 + fmt.Println(a) + + // 指针 + var p *int = &a + *p = 11 + fmt.Println(a) + + // Rune -> int32 + fmt.Println(len("🍉🍇🍑🍓🥝")) + fmt.Println(len([]rune("🍉🍇🍑🍓🥝"))) + + // map + var o = map[string]int{ + "a": 3, + "b": 4, + } + fmt.Println(o) + + // 判断是否存在某个 key + if v, ok := o["a"]; ok { + fmt.Println("a exist", v) + } + + fmt.Println(RED) +} diff --git a/go/hello/routine.go b/go/hello/routine.go new file mode 100644 index 0000000..d9a9baa --- /dev/null +++ b/go/hello/routine.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func hello() { + fmt.Println("hello, shanyue") +} + +func main() { + hello() + go hello() + fmt.Println("main function") +} diff --git a/node-native/encoding/index.js b/node-native/encoding/index.js index 9cdb679..53f207b 100644 --- a/node-native/encoding/index.js +++ b/node-native/encoding/index.js @@ -2,6 +2,7 @@ const iconv = require('iconv-lite') const { Response } = require('undici') const str = '山月' +const emoji = '🍉🍇' function f1 () { new Response(str).arrayBuffer(b => new Uint8Array(b)).then(b => { @@ -9,10 +10,15 @@ function f1 () { }) } +// 在 iconv 中可以将字符串用以 utf16 编码,而 TextEncoder 不行 function f2 () { const b = iconv.encode(str, 'utf16') console.log(b) console.log(new Uint8Array(b)) + + const c = iconv.encode(emoji, 'utf16') + console.log(c) + console.log(new Uint8Array(c)) } function f3 () { @@ -21,4 +27,8 @@ function f3 () { console.log(b) } +function f4 () { + iconv.decode('', 'utf16') +} + f2() \ No newline at end of file diff --git a/node-server/koa/index.js b/node-server/koa/index.js index e3cc288..5a88ffe 100644 --- a/node-server/koa/index.js +++ b/node-server/koa/index.js @@ -33,9 +33,23 @@ f = ctx => { f = (ctx) => { // Koa 通过 parseurl 这个库进行 URL 解析 + // parseurl 将只会解析一次,进行缓存,不会重复解析,可查看其 fresh 方法 + // https://github.com/pillarjs/parseurl/blob/master/index.js#L153 + const a = ctx.query.a + const b = ctx.query.b + const path = ctx.path ctx.body = ctx.query } +f = (ctx) => { + const charset = ctx.request.charset + + const b = ctx.request.charset + + ctx.request.accepts('json') + ctx.body = charset +} + handleRequest = f app.use(handleRequest)