Skip to content

Commit

Permalink
feat: go
Browse files Browse the repository at this point in the history
  • Loading branch information
shfshanyue committed Mar 27, 2022
1 parent 6131577 commit 22a3660
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
23 changes: 23 additions & 0 deletions go/hello/defer.go
Original file line number Diff line number Diff line change
@@ -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())
}
3 changes: 3 additions & 0 deletions go/hello/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module hello

go 1.17
38 changes: 38 additions & 0 deletions go/hello/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 13 additions & 0 deletions go/hello/routine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "fmt"

func hello() {
fmt.Println("hello, shanyue")
}

func main() {
hello()
go hello()
fmt.Println("main function")
}
10 changes: 10 additions & 0 deletions node-native/encoding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ 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 => {
console.log(b)
})
}

// 在 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 () {
Expand All @@ -21,4 +27,8 @@ function f3 () {
console.log(b)
}

function f4 () {
iconv.decode('', 'utf16')
}

f2()
14 changes: 14 additions & 0 deletions node-server/koa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 22a3660

Please sign in to comment.