Skip to content

Commit cec87e0

Browse files
author
cy422396350
committed
add new time package,regexp package code
1 parent 3d8f6b5 commit cec87e0

File tree

8 files changed

+254
-85
lines changed

8 files changed

+254
-85
lines changed

base/reflect/reflect_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@ import (
55
"fmt"
66
"reflect"
77
"testing"
8-
"unsafe"
98
)
109

1110
type TestStruct struct {
1211
A int `json:"a"`
1312
B string `json:"b"`
14-
c string `json:"c"`
13+
C string `json:"c"`
1514
}
1615

1716
// 通过反射修改非导出字段
1817
func TestChangeNotExportFiled(t *testing.T) {
1918
var r TestStruct
20-
// 获取字段对象
21-
v := reflect.ValueOf(&r).Elem().FieldByName("c")
22-
// 构建指向该字段的可寻址(addressable)反射对象
23-
rv := reflect.NewAt(v.Type(), unsafe.Pointer(v.UnsafeAddr())).Elem()
24-
// 设置值
25-
fv := reflect.ValueOf("pibigstar")
26-
rv.Set(fv)
19+
r.A = 100
20+
r.B = "B"
21+
r.C = "c"
22+
getValue := reflect.ValueOf(r)
23+
if getValue.Kind() != reflect.Struct {
24+
panic("need struct kind")
25+
}
26+
getType := reflect.TypeOf(r)
27+
// 或者 getType := getValue.Type()
28+
for i := 0; i < getValue.NumField(); i++ {
29+
t.Logf("name: %s, type: %s, value: %v\n", getType.Field(i).Name, getValue.Field(i).Type(), getValue.Field(i).Interface())
30+
}
2731

2832
t.Logf("%+v", r)
2933
}

base/unsafe/unsafe_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
// 任何指针都可以转换为unsafe.Pointer
1111
// unsafe.Pointer可以转换为任何指针
1212
func TestPointer(t *testing.T) {
13-
i := 10
14-
ip := &i
13+
i := 10 // 声明一个地址和数据
14+
ip := &i // 取得这个数据的地址
1515
// 将 *int 转为 *float64
16-
var fp = (*float64)(unsafe.Pointer(ip))
17-
18-
*fp = *fp * 3
19-
t.Log(i)
16+
var fp = (*float64)(unsafe.Pointer(ip)) // 把地址值给fp
17+
t.Logf("%p", &i)
18+
*fp = *fp * 3 // 把地址值的数据乘以3
19+
t.Logf("%p", fp) // float64
2020
}
2121

2222
// uintptr可以转换为unsafe.Pointer

design/singleton/go_single.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@ package singleton
22

33
import "sync"
44

5+
type singleton struct{}
6+
57
var (
68
goInstance *Instance
9+
ins *singleton
710
once sync.Once
811
)
912

1013
// 使用go 实现单例模式
11-
func GoInstance(name string) *Instance {
12-
if goInstance == nil {
13-
once.Do(func() {
14-
goInstance = &Instance{
15-
Name: name,
16-
}
17-
})
18-
}
19-
return goInstance
14+
func GetIns() *singleton {
15+
once.Do(func() {
16+
ins = &singleton{}
17+
})
18+
return ins
2019
}

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/antchfx/xpath v1.1.8 // indirect
1919
github.com/astaxie/beego v1.12.0 // indirect
2020
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
21+
github.com/charmbracelet/lipgloss v0.1.1
2122
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
2223
github.com/coreos/etcd v3.3.18+incompatible
2324
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
@@ -36,6 +37,7 @@ require (
3637
github.com/gocolly/colly v1.2.0
3738
github.com/gofrs/uuid v3.2.0+incompatible // indirect
3839
github.com/goinggo/mapstructure v0.0.0-20140717182941-194205d9b4a9
40+
github.com/golang-module/carbon v1.3.7 // indirect
3941
github.com/golang/mock v1.3.1
4042
github.com/golang/protobuf v1.4.2 // indirect
4143
github.com/google/go-cmp v0.4.0
@@ -81,14 +83,14 @@ require (
8183
github.com/skip2/go-qrcode v0.0.0-20190110000554-dc11ecdae0a9
8284
github.com/smallnest/rpcx v0.0.0-20170601021420-329cf0078113
8385
github.com/smartwalle/alipay/v3 v3.0.9
86+
github.com/smartystreets/goconvey v1.6.4
8487
github.com/soheilhy/cmux v0.1.4
8588
github.com/sony/sonyflake v1.0.0
8689
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94
8790
github.com/temoto/robotstxt v1.1.1 // indirect
8891
github.com/uber/jaeger-client-go v2.23.1+incompatible
8992
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
9093
github.com/unidoc/unioffice v1.2.0
91-
github.com/varstr/uaparser v0.0.0-20170929040706-6aabb7c4e98c
9294
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
9395
github.com/xdg/stringprep v1.0.0 // indirect
9496
github.com/xuri/excelize v1.4.0
@@ -101,7 +103,6 @@ require (
101103
golang.org/x/net v0.0.0-20200602114024-627f9648deb9
102104
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
103105
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
104-
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9 // indirect
105106
golang.org/x/text v0.3.2
106107
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
107108
google.golang.org/appengine v1.6.6 // indirect

go.sum

Lines changed: 212 additions & 4 deletions
Large diffs are not rendered by default.

pprof/main.go

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,12 @@ package main
22

33
import (
44
"fmt"
5-
"log"
6-
"net/http"
5+
"github.com/charmbracelet/lipgloss"
76
_ "net/http/pprof"
8-
"os"
9-
"path/filepath"
10-
"time"
11-
12-
"github.com/varstr/uaparser"
137
)
148

15-
const hostPort = ":8080"
9+
var style = lipgloss.NewStyle().Bold(false).Foreground(lipgloss.Color("#FAFAFA")).Background(lipgloss.Color("#7D56F4")).PaddingTop(2).PaddingLeft(4).Width(22)
1610

1711
func main() {
18-
http.HandleFunc("/hello", Hello)
19-
fmt.Println("Starting server on", hostPort)
20-
if err := http.ListenAndServe(hostPort, nil); err != nil {
21-
log.Fatalf("HTTP server failed: %v", err)
22-
}
23-
}
24-
25-
func Hello(w http.ResponseWriter, r *http.Request) {
26-
start := time.Now()
27-
tags := getStatsTags(r)
28-
duration := time.Since(start)
29-
fmt.Println(tags, duration)
30-
}
31-
32-
func getStatsTags(r *http.Request) map[string]string {
33-
userBrowser, userOS := parseUserAgent(r.UserAgent())
34-
stats := map[string]string{
35-
"browser": userBrowser,
36-
"os": userOS,
37-
"endpoint": filepath.Base(r.URL.Path),
38-
}
39-
40-
hostName, _ := os.Hostname()
41-
42-
if hostName != "" {
43-
stats["host"] = hostName
44-
}
45-
return stats
46-
}
47-
48-
func parseUserAgent(uaString string) (browser, os string) {
49-
ua := uaparser.Parse(uaString)
50-
51-
if ua.Browser != nil {
52-
browser = ua.Browser.Name
53-
}
54-
if ua.OS != nil {
55-
os = ua.OS.Name
56-
}
57-
58-
return browser, os
12+
fmt.Println(style.Render("Hello, kitty."))
5913
}

utils/hystrix/hystrix_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func TestHystrix(t *testing.T) {
1212
hystrix.ConfigureCommand("test", hystrix.CommandConfig{
13-
MaxConcurrentRequests: 1, // 最大并发量
13+
MaxConcurrentRequests: 10, // 最大并发量
1414
Timeout: 3000, // 单次请求超时时间,如果超过3s将认为服务不可用
1515
RequestVolumeThreshold: 1, // 10秒内如果出现1次错误就触发熔断
1616
ErrorPercentThreshold: 1, // 按百分比,如果出现1%的错误就触发熔断

utils/timex/timex_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ func TestTimeParse(t *testing.T) {
4545
}
4646

4747
func TestTimeFirstAndLast(t *testing.T) {
48-
now := time.Now()
49-
t.Log(FirstMonth(now))
50-
t.Log(FirstMonthUnix(now))
51-
t.Log(LastMonth(now))
52-
t.Log(LastMonthUnix(now))
48+
st, err := ParseTime("2020-09-10 15:22:00")
49+
if err != nil {
50+
t.Error(err)
51+
}
52+
t.Log(FirstMonth(st))
53+
t.Log(FirstMonthUnix(st))
54+
t.Log(LastMonth(st))
55+
t.Log(LastMonthUnix(st))
5356
}

0 commit comments

Comments
 (0)