随笔分类 -  go

上一页 1 2 3 4 5 6 7 ··· 16 下一页

golang三大基础mock大法
摘要:一、使用gomonkey来mock函数和方法 1、mock函数 gomonkey.ApplyFunc(target,double) 其中target是被mock的目标函数,double是用户重写的函数。 注意点:重写的函数要和原函数入参和出参保持一致,否则会报错。 2、mock方法 gomonkey 阅读全文

posted @ 2021-10-13 14:16 ExplorerMan 阅读(2152) 评论(0) 推荐(0)

用vscode开发调试golang超简单教程
摘要:目录 一、下载并安装vscode 二、安装Go插件 三、下载调试工具 四、打开现有go工程文件夹或新建go项目即可进行调试 五、问题处理 首先安装golang开发环境,这个不用说。 到这个页面下载golang 的安装包 https://studygolang.com/dl ,再安装就可以了。 一、下 阅读全文

posted @ 2021-10-12 21:25 ExplorerMan 阅读(5291) 评论(0) 推荐(0)

go语言 判断一个实例是否实现了某个接口interface
摘要:package main import "fmt" type Animal interface { run() walk() } type Dog struct { Id int } func (dog Dog) run() { fmt.Printf("I am Dog,I can Run!\n") 阅读全文

posted @ 2021-10-09 17:53 ExplorerMan 阅读(145) 评论(0) 推荐(0)

golang,函数参数传递的sync.Mutex不是指针会怎么样
摘要:package main import ( "fmt" "sync")var a = 1func main() { lock := sync.Mutex{} wg := &sync.WaitGroup{} wg.Add(2) go t(lock,wg) go t(lock,wg) wg.Wait() 阅读全文

posted @ 2021-09-09 11:24 ExplorerMan 阅读(221) 评论(0) 推荐(0)

golang读写锁RWMutex
摘要:读写锁是针对读写的互斥锁 基本遵循两大原则: 1、可以随便读,多个goroutine同时读 2、写的时候,啥也不能干。不能读也不能写 RWMutex提供了四个方法: func (*RWMutex) Lock // 写锁定 func (*RWMutex) Unlock // 写解锁 func (*RW 阅读全文

posted @ 2021-09-09 11:06 ExplorerMan 阅读(452) 评论(0) 推荐(0)

golang_并发安全: slice和map并发不安全及解决方法
摘要:golang_并发安全: slice和map并发不安全及解决方法 Grayan · 2020-07-21 15:32:48 · 1771 次点击 · 预计阅读时间 1 分钟 · 不到1分钟之前 开始浏览 这是一个创建于 2020-07-21 15:32:48 的文章,其中的信息可能已经有所发展或是发 阅读全文

posted @ 2021-09-09 10:55 ExplorerMan 阅读(502) 评论(0) 推荐(0)

什么情况下需要用到互斥锁sync.Mutex?
摘要:package mainimport ( "fmt" "runtime" "time")var a intfunc main() { runtime.GOMAXPROCS(runtime.NumCPU()) ch := make(chan int) for i :=0;i<1000;i++{ go 阅读全文

posted @ 2021-09-09 10:53 ExplorerMan 阅读(138) 评论(0) 推荐(0)

GOMAXPROCS你设置对了吗?
摘要:1. 前言 有圈子的朋友介绍 uber-go/automaxprocs, 我才发现之前在docker中, Golang程序设置的GOMAXPROCS不正确,有必要在重新回顾一下了。 2. Go 调度器: M, P 和 G 我们知道在Go scheduler中,G代表goroutine, P代表Log 阅读全文

posted @ 2021-09-07 21:15 ExplorerMan 阅读(2862) 评论(0) 推荐(0)

容器资源可见性问题与 GOMAXPROCS 配置
摘要:Go 程序启动时候会根据 CPU 数量设置 GOMAXPROCS,比如下面的程序中在 8 核处理下输出为 8。 package main import ( "runtime" ) func main() { println(runtime.NumCPU()) } 当我们使用 Docker 的时候也是 阅读全文

posted @ 2021-09-07 21:14 ExplorerMan 阅读(793) 评论(0) 推荐(0)

Go语言参数校验(go-playground / validator)——基本使用
摘要:Go语言没有像Java一样的注解快速进行参数的校验,但可以通过struct tag(结构体标签)进行序列化。常用的如: type User struct { ID string `json:"id"` Name string `json:"name"` Age string `json:"age"` 阅读全文

posted @ 2021-08-18 16:11 ExplorerMan 阅读(1328) 评论(0) 推荐(1)

自定义json unmarshaler返回空字段
摘要:我已经实现了一个自定义JSON解组器,但由于某种原因它不会返回正确的值 - 所有字段都会返回零。自定义json unmarshaler返回空字段 例如: type test struct { t string } func New(data string) (*test, error) { retu 阅读全文

posted @ 2021-08-17 11:24 ExplorerMan 阅读(251) 评论(0) 推荐(0)

golang的select典型用法
摘要:golang 的 select 的功能和 select, poll, epoll 相似, 就是监听 IO 操作,当 IO 操作发生时,触发相应的动作。 示例: ch1 := make (chan int, 1) ch2 := make (chan int, 1) ... select { case 阅读全文

posted @ 2021-07-30 14:37 ExplorerMan 阅读(931) 评论(0) 推荐(0)

GoMock框架使用指南
摘要:序言 要写出好的测试代码,必须精通相关的测试框架。对于Golang的程序员来说,至少需要掌握下面四个测试框架: GoConvey GoStub GoMock Monkey 读者通过前面三篇文章的学习可以对框架GoConvey和GoStub优雅的组合使用了,本文将接着介绍第三个框架GoMock的使用方 阅读全文

posted @ 2021-07-29 14:17 ExplorerMan 阅读(807) 评论(0) 推荐(0)

golang对结构体排序,重写sort
摘要:package main import ( "fmt" "sort" ) type Log struct { UserID int Message string Num float64 CreateTime string } type Wrapper struct { log []Log by fu 阅读全文

posted @ 2021-07-28 16:12 ExplorerMan 阅读(224) 评论(0) 推荐(0)

Go语言开发Prometheus Exporter示例
摘要:一、Prometheus中的基本概念Prometheus将所有数据存储为时间序列,这里先来了解一下prometheus中的一些基本概念 指标名和标签每个时间序列都由指标名和一组键值对(也称为标签)唯一标识。 metric的格式如下: <metric name>{<label name>=<label 阅读全文

posted @ 2021-07-27 19:35 ExplorerMan 阅读(1273) 评论(0) 推荐(0)

golang 字符串拼接性能比较
摘要:背景最近在做一个服务发现/注册的agent, 各个服务需要通过这个agent来注册自己的服务,在完成开发后,测试性能时发现性能达不到要求,通过pprof 来确认cpu主要耗费在gc上,分析结果主要是由于字符串拼接导致,故需要测试一下字符串拼接的几种方法的性能;12字符串拼接的几种方法直接使用加号进行 阅读全文

posted @ 2021-07-27 19:27 ExplorerMan 阅读(231) 评论(0) 推荐(0)

golang中的strings.Compare
摘要:golang中字符串操作函数strings.Compare package main import ( "fmt" "strings" ) //golang字符串操作func main(){ s := "Hello world hello world" str := "Hello" //var s 阅读全文

posted @ 2021-07-27 19:25 ExplorerMan 阅读(399) 评论(0) 推荐(0)

Golang 程序中实现优雅关闭 HTTP SERVER
摘要:Golang 中实现一个 HTTP SERVER 异常的简单,利用标准库 net/http 的实现仅需数行代码即可,但是一个生产环境可用的 HTTP SERVER 还必须考虑更多的问题,其中如何实现优雅关闭 HTTP SERVER 是一个必须要处理的问题。这里所说的 优雅 即是指在 HTTP SER 阅读全文

posted @ 2021-07-27 11:54 ExplorerMan 阅读(402) 评论(0) 推荐(0)

Golang的time.NewTicker周期性定时器使用案例
摘要:Ticker是一个周期触发定时的计时器,它会按照一个时间间隔往channel发送系统当前时间,而channel的接收者可以以固定的时间间隔从channel中读取事件。 一.通过NewTicker函数定义定时器 package main import ( "fmt" "time" ) func mai 阅读全文

posted @ 2021-07-26 20:22 ExplorerMan 阅读(455) 评论(0) 推荐(0)

彻底搞懂golang的GOROOT和GOPATH
摘要:目录 1、GOPATH 和 GOROOT 2、修改 GOPATH 和 GOROOT 3、HelloWord——GOPATH版 4、一些踩坑经验 1、GOPATH 和 GOROOT不同于其他语言,go中没有项目的说法,只有包, 其中有两个重要的路径,GOROOT 和 GOPATH Go开发相关的环境变 阅读全文

posted @ 2021-07-20 11:23 ExplorerMan 阅读(4675) 评论(0) 推荐(0)

上一页 1 2 3 4 5 6 7 ··· 16 下一页

导航