
go
和风说话的青苔
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
包cron github.com/robfig/cron/v3
定时任务设置时区原创 2022-09-20 16:25:46 · 692 阅读 · 1 评论 -
go包go-redis lua脚本
package mainimport ( "context" "errors" "fmt" "github.com/go-redis/redis/v8")// go get github.com/go-redis/redis/v8var ctx = context.Background()func main() { ExampleClient()}func ExampleClient() { rdb := redis.NewClient(&redis.Option原创 2022-04-27 12:42:49 · 1654 阅读 · 0 评论 -
go包dig依赖注入
package mainimport ( "errors" "fmt" "go.uber.org/dig")func main() { c := BuildContainer() if err := c.Invoke(func(i *PersonRepositor) { i.Hello() }); err != nil { fmt.Println("Invoke", err) return } if err := c.Invoke(func(i *DB2) {原创 2022-04-10 15:27:12 · 635 阅读 · 0 评论 -
golang匿名结构体赋值
type UserToken struct { Certificate string `json:"certificate"` UserId string `json:"user_id"` UserName string `json:"user_name"` Role struct { Id string `json:"id"` Name string `json:"name"` } `json:"role"` Extra *struct { NS原创 2022-03-25 14:57:04 · 1279 阅读 · 0 评论 -
go包sarama 连接kafka
package resourceimport ( "context" "fmt" "ms_web/configs" "github.com/Shopify/sarama")type Kafka struct{ client sarama.SyncProducer Topic string}func NewKafka(ctx context.Context, conf *configs.Config) (*Kafka, error) { config := sarama.Ne原创 2022-03-24 17:28:55 · 3247 阅读 · 0 评论 -
go包https://github.com/alicfeng/mysql_markdown生成数据库表结构markdown说明文档
github地址生成数据库表结构markdowngo run mysql_markdown.go -h hostUrl -u 用户名 -p 密码 -d 数据库名 -P 3306原创 2021-12-17 10:08:15 · 714 阅读 · 0 评论 -
go包amqp
package mainimport ( "fmt" "github.com/streadway/amqp")// amqp/client/main.gofunc main(){ co()}func co(){ conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/test") if err != nil { fmt.Println("InitRabbitMq Dial", err) return } c原创 2021-11-15 10:06:45 · 443 阅读 · 0 评论 -
go-zero的批量更新插入
func sl(){ data := make([]*DyOrderDetail,2) data[0] = &DyOrderDetail{ Remark: "rr11", OrderId: "id11", ParentOrderId: "pid1", } data[1] = &DyOrderDetail{ Remark: "rr22", OrderId: "id22", ParentOrderId: "pid1", } conn := sqlx.NewMy原创 2021-11-11 16:01:28 · 1906 阅读 · 3 评论 -
golang资源竞争-race、逃逸分析
package mainimport ( "fmt" "runtime" "sync")func main(){ fmt.Println(runtime.GOMAXPROCS(0)) n := 0 wg := sync.WaitGroup{} wg.Add(10) for i:=0;i<10;i++{ go func(){ defer wg.Done() n = n+1 fmt.Println(n) }() } wg.Wait() fmt.Pr原创 2021-11-03 14:23:25 · 277 阅读 · 0 评论 -
go-reflect根据结构体生成sql
package mainimport ( "errors" "fmt" "reflect")type Info struct { Name string Price int `json:"price" from:"pri"`}func main() { info := Info{ Name: "name1", Price: 1, } err, sql := reflectSql(info) fmt.Println(err, sql) // <nil>原创 2021-10-27 11:11:24 · 771 阅读 · 0 评论 -
go包base64解密
// encoding/base64"decoded, err := base64.StdEncoding.DecodeString("待解密字符串")var info request.TmReceiverInfoif err == nil { decodeStr := string(decoded) enEscapeData, _ := url.QueryUnescape(decodeStr) json.Unmarshal([]byte(enEscapeData), &info)}.原创 2021-10-18 19:18:19 · 885 阅读 · 0 评论 -
go包resize、draw图片缩放、合并图片
package mainimport ( "fmt" "image" "image/draw" "image/jpeg" "net/http" "os" "strconv" "time" "github.com/nfnt/resize" "golang.org/x/sync/errgroup")func main() { img := strconv.Itoa(int(time.Now().UnixNano())) + ".jpeg" f, err := os.Creat原创 2021-10-14 11:41:47 · 2083 阅读 · 0 评论 -
go依赖包出现版本不兼容的问题
大概是说原因是 google.golang.org/grpc 1.26 后的版本是不支持 clientv3 的。也就是说要把这个改成 1.26 版本的就可以了。在 go.mod 里加上:replace google.golang.org/grpc => google.golang.org/grpc v1.26.0原创 2021-10-09 11:31:53 · 4081 阅读 · 0 评论 -
go包github.com/tal-tech/go-queue/dq延时队列
使用参考package mainimport ( "fmt" "strconv" "time" "github.com/tal-tech/go-zero/core/stores/redis" "github.com/tal-tech/go-queue/dq")func main() { producer := dq.NewProducer([]dq.Beanstalk{ { Endpoint: "localhost:11300", Tube: "tube"原创 2021-10-08 16:43:09 · 310 阅读 · 0 评论 -
go包github.com/google/go-querystring/query 转义url参数
package mainimport ( "fmt" "github.com/google/go-querystring/query")// go get -u github.com/google/go-querystring/queryfunc main() { in := Info{ Name: "aq", Age: 2, Comment: "测试", } v, err := query.Values(in) // 会自动a-z排序、转义 fmt原创 2021-09-09 19:01:13 · 354 阅读 · 0 评论 -
go包github.com/jinzhu/copier
package mainimport ( "fmt" "time" "github.com/jinzhu/copier")// 结构体赋值// go get -u github.com/jinzhu/copierfunc main() { in := Info2{ ID: 1, LastModificationTime: time.Now(), } in2 := &Info{} copier.Copy(in2, in) f原创 2021-09-09 17:28:16 · 715 阅读 · 0 评论 -
go下载文件The system cannot find the path specified.
如果os.Create出现The system cannot find the path specified.需要先创建目录:os.MkdirAll(filePath, os.ModeDir)filePath := "./files/export" filename := ("朋友圈任务完成明细" + time.Now().Format("-20060102150405")) + ".xlsx" filename = filepath.Join(filePath, filename) err = o原创 2021-09-07 11:45:54 · 3430 阅读 · 0 评论 -
go 包 性能分析工具 Pyroscope
启动 Pyroscope 服务:docker run -it -p 4040:4040 pyroscope/pyroscope:latest server启动后浏览器打开:localhost:4040 即可访问客户端启动agentimport "github.com/pyroscope-io/pyroscope/pkg/agent/profiler"// 在项目里入以下代码即可profiler.Start(profiler.Config{ ApplicationName: "sim原创 2021-08-16 17:02:25 · 731 阅读 · 0 评论 -
go tool pprof
package mainimport ( "log" "net/http" _ "net/http/pprof" "time")var datas []stringfunc main() { go func() { for { log.Printf("len: %d", Add("go-programming-tour-book")) time.Sleep(time.Second) } }() // http://127.0.0.1:6060/debug/p原创 2021-08-16 11:46:30 · 212 阅读 · 0 评论 -
go sql转结构体 sql2struct
http://sql2struct.atotoa.com/原创 2021-08-09 14:05:42 · 1082 阅读 · 0 评论 -
内存对齐原则
package mainimport ( "fmt" "time" "unsafe")type OfSize struct { Name uint64 Ct uint32 Bt byte}func main() { var hh OfSize fmt.Println(unsafe.Sizeof(hh)) // 16 字节 fmt.Println(unsafe.Sizeo原创 2021-08-05 10:09:48 · 105 阅读 · 0 评论 -
go包 interface解析到结构体github.com/mitchellh/mapstructure
package mainimport ( "fmt" "github.com/mitchellh/mapstructure")// go get github.com/mitchellh/mapstructure// 将interface解析到结构体上func main() { InterfaceToStruct() Squash() TypeErr() Remain()}type Person struct { Name string Age int原创 2021-08-05 09:37:33 · 569 阅读 · 0 评论 -
go包 golang.org/x/sync/errgroup
package mainimport ( "context" "errors" "fmt" "runtime" "time" "golang.org/x/sync/errgroup")func main() { group() for { fmt.Println(runtime.NumGoroutine()) time.Sleep(time.Second) }}func group() { var eg errgroup.Group eg.Go(func原创 2021-08-04 14:38:01 · 675 阅读 · 0 评论 -
go post请求
import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" "strings")//PostJSON post json 数据请求func PostJSON(uri string, obj interface{}) ([]byte, error) { jsonData := make([]byte, 0, 0) if str, ok := obj.(string); ok { jsonData = []byte(st原创 2021-08-03 16:00:48 · 2938 阅读 · 0 评论 -
go库excel https://github.com/xuri/excelize
中文文档原创 2021-08-02 14:07:27 · 1584 阅读 · 0 评论 -
go包 github.com/gomodule/redigo/redis
package mainimport ( "fmt" "github.com/gomodule/redigo/redis" "time")// go get github.com/gomodule/redigo/redisvar Redis *RedisPoolfunc main(){ // 初始化redis连接池 Redis = &RedisPool{} Redis.InitRedisPool("") structAdd() structValues()}原创 2021-08-02 10:56:24 · 2087 阅读 · 0 评论 -
golang http请求出现400 Bad Request net/url
这是因为时间没有转码,要使用url.QueryEscape()// import "net/url"start := url.QueryEscape("2021-07-13 14:20:48")match_end_time := url.QueryEscape("2021-07-13 14:25:48")res, err := ReadMatchUser("GET", "http://xxxxxx/api/v1/dzzs-app/match/crowd?match_start_time="+star原创 2021-07-19 17:01:15 · 1801 阅读 · 0 评论 -
go包-取交集、差集包 github.com/fatih/set
package mainimport ( "fmt" "github.com/fatih/set")// go get github.com/fatih/setfunc main() { section1 := []string{"33", "22", "11"} section2 := []string{"33", "22","66"} hh := DifferenceString(section1, section2) fmt.Println(hh) // [11]}//原创 2021-07-14 15:32:50 · 531 阅读 · 0 评论 -
go-zero使用笔记
生成单体服务代码goctl api new leaf 此命令就生成了leaf的web项目代码,目录结构如下:其中leaf是web项目目录,etc里是配置,handler是路由处理函数和路由,logic是路由的逻辑处理函数,leaf.go是项目入口文件创建微服务项目mkdir mall && cd mallmkdir -p user/rpc && cd user/rpc添加user.proto文件,增加getUser方法 vim mall/user/rp原创 2021-05-21 15:49:31 · 908 阅读 · 1 评论 -
go调用rpc方法报408 context deadline exceeded
如果调用所有rpc方法都是408,可能是rpc服务和调用rpc方法的服务 没有注册在同一个服务发现中,如不在同一个consul里。原创 2021-05-21 11:08:50 · 2613 阅读 · 2 评论 -
go超时控制
func Close() { ch := make(chan struct{}) go func() { // TODO s.Close() time.Sleep(time.Second*2) close(ch) }() select { case <-ch: fmt.Println("close ok") case <-time.After(time.Second): fmt.Println("close timeout") }}原创 2021-05-19 17:04:31 · 94 阅读 · 0 评论 -
go单元测试
在goland中,右键选择GO TO -> Test,即可自动生成test文件原创 2021-05-19 15:53:04 · 186 阅读 · 0 评论 -
goland下载后运行没有反应
因为误删东西,导致goland无法启动,重启、重装都不行;下载 Everything搜索goland,删除所有搜索到的,再重装就可打开原创 2021-05-12 13:44:12 · 1259 阅读 · 0 评论 -
go结构体排序
package mainimport ( "fmt" "sort" "time")type Student struct { CreatedAt time.Time}type res []*Studentfunc (a res) Len() int { return len(a) }func (a res) Swap(i, j int) { a[i], a[j] = a[j], a[i] }func (a res) Less(i, j int)原创 2021-04-14 14:49:37 · 313 阅读 · 0 评论 -
go 解析token
//import "encoding/base64"func token() { hh, err := base64.RawURLEncoding.DecodeString("eyJuYW1lIjoiem55Iiwib3BlbmlkIjpudWxsLCJuaWNrbmFtZSI6bnVsbC**********oHpm4UCJhdXRocyI6WyJzYS5xYSJdLCJpYXQiOjE2MTE5MDY4NzgsImV4cCI6MTYxNDg3OH0") if err != nil { fmt.原创 2021-01-29 16:07:18 · 922 阅读 · 0 评论 -
熔断器hystrix、sentinel
hstrix:// 超时代码// import github.com/afex/hystrix-go/hystrix// 1.配置configconfigA := hystrix.CommandConfig{ Timeout: 1000,}// 2.配置commandhystrix.ConfigureCommand("getProds", configA)// 3.执行Do方法var prodRes *models.ProdListResponseerr := hystrix.Do("原创 2021-01-28 14:05:11 · 340 阅读 · 0 评论 -
go - time 获取指定时间所在月份的第一天0点
//获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。func GetFirstDateOfMonth(d time.Time) time.Time { d = d.AddDate(0, 0, -d.Day()+1) return GetZeroTime(d)}//获取传入的时间所在月份的最后一天,即某月最后一天的23:59:59。如传入time.Now(), 返回当前月份的最后一天0点时间。func GetLastDateOfMo原创 2021-01-08 11:49:27 · 1089 阅读 · 0 评论 -
go去掉proto文件中的omitempty
/d/work/src/新建文件夹/新建文件夹/falcon/src/srv/order (dev-bbtc/v0.0.3)$ ls proto/order/*.pb.go | xargs -n1 -IX bash -c 'sed s/,omitempty// X > X.tmp && mv X{.tmp,}'去掉proto/order/*.pb.go的 ,omitempty原创 2020-12-23 17:20:44 · 1904 阅读 · 0 评论 -
go中的break label
break label,break的跳转标签(label)必须放在循环语句for前面,并且在break label跳出循环不再执行for循环里的代码。package mainimport ( "fmt")func main() { fmt.Println("break label")EXIT: // label: 下面必须是循环 for i := 0; i < 3; i++ { fmt.Println(i) if i == 2 { // 当i=2时,原创 2020-11-23 14:18:39 · 468 阅读 · 0 评论 -
go-gorm
package mainimport ( "encoding/json" "fmt" "time" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql")var MainDB *gorm.DBfunc main() { url := "test:123456@tcp(127.0.0.1:3306)/blogger?charset=utf8mb4&parseTime=True&loc=L原创 2020-11-22 13:44:21 · 328 阅读 · 0 评论