随笔分类 -  Go

1 2 3 4 5 下一页

gorm使用事务的2种方式
摘要:方式1:基本事务(常用) err := db.Transaction(func(tx *gorm.DB) error { // 执行事务操作 return nil }) 方式2:手动事务 // 开始事务 tx := db.Begin() defer func() { if r := recover( 阅读全文

posted @ 2025-11-21 08:57 王景迁 阅读(10) 评论(0) 推荐(0)

go http request WithContext和NewRequestWithContext方法
摘要:go有2个方法设置request的context,实现上下文在同一个request中传递。方法1:WithContext func (r *Request) WithContext(ctx context.Context) *Request 浅拷贝旧的request,返回新的request,新老re 阅读全文

posted @ 2025-11-07 09:11 王景迁 阅读(20) 评论(0) 推荐(0)

go 并发赋值安全性
摘要:32/64位机器读写占用空间小于或等于32/64位的变量是原子操作,超过时属于非原子操作。 安全的赋值类型基本数据类型:字节型、布尔型、整型、浮点型、字符型其他数据类型:指针、函数 不安全的赋值类型基本数据类型:复数型、字符串其他数据类型:数组、切片、字典、通道、接口、结构体 可以通过atomic. 阅读全文

posted @ 2025-10-19 16:41 王景迁 阅读(6) 评论(0) 推荐(0)

go http实现长连接
摘要:服务端 package main import ( "net/http" "time" ) func handler(w http.ResponseWriter, r *http.Request) { f := w.(http.Flusher) for { w.Write([]byte("1\n") 阅读全文

posted @ 2025-08-28 22:32 王景迁 阅读(18) 评论(0) 推荐(0)

go http trace分析耗时
摘要:package main import ( "crypto/tls" "fmt" "log" "net/http" "net/http/httptrace" "time" ) func main() { req, _ := http.NewRequest("GET", "https://www.ba 阅读全文

posted @ 2025-08-17 20:45 王景迁 阅读(13) 评论(0) 推荐(0)

go select选择case分析
摘要:场景 条件 执行default 存在default,且无case满足。 阻塞 没有default,且无case满足。 选择单个case执行 如果只有1个case满足,那么直接执行;如果有多个case满足,那么随机选择执行,通过随机来保证公平性。 阅读全文

posted @ 2025-07-27 07:53 王景迁 阅读(9) 评论(0) 推荐(0)

go map不能使用指针作为key
摘要:package main import "fmt" type student struct { name string age int } func main() { m := make(map[*student]bool) s1 := &student{ name: "a", age: 17, } 阅读全文

posted @ 2025-07-18 08:16 王景迁 阅读(14) 评论(0) 推荐(0)

go 设置http请求超时时间
摘要:10.11.12.13不真实存在,ping会超时。 使用 http.Client 设置全局超时 package main import ( "net/http" "time" ) func main() { client := &http.Client{ Timeout: 5 * time.Seco 阅读全文

posted @ 2025-07-18 08:09 王景迁 阅读(56) 评论(0) 推荐(0)

go 切片删除某个元素
摘要:package main import "fmt" func delete(s []int, num int) []int { for i := 0; i < len(s); i++ { if s[i] == num { s = append(s[:i], s[i+1:]...) i-- } } r 阅读全文

posted @ 2025-07-12 17:31 王景迁 阅读(11) 评论(0) 推荐(0)

go 使用options扩展参数
摘要:package main import ( "fmt" ) type Config struct { path string isUseFile bool } type ConfigOption func(config *Config) error func WithPath(path string 阅读全文

posted @ 2025-06-22 16:17 王景迁 阅读(10) 评论(0) 推荐(0)

go url parse
摘要:url = schema(http/https) + "//" + host(ip:port/domain:port) + path(/xxx) + "?" + rawQuery(xxx=xxx) package main import ( "fmt" "net/url" ) func parseU 阅读全文

posted @ 2025-06-22 11:50 王景迁 阅读(20) 评论(0) 推荐(0)

go context value保存map
摘要:go context虽然只能保存一对key和value,但是支持any;虽然获取value时支持不断从父context中找,但是无法获取所有kv。 package main import ( "maps" "context" "fmt" ) // context推荐使用自定义结构体类型作为key,与 阅读全文

posted @ 2025-06-21 11:00 王景迁 阅读(19) 评论(0) 推荐(0)

go https代理认证
摘要:curl --proxy-header "代理认证信息" --proxy "代理地址" https://xxx 这里--proxy-header和普通header -H不等价,虽然两者类型相同,但是前者只发送给代理服务端(仅限https场景),后者只发送给服务端。 go Transport实现了ht 阅读全文

posted @ 2025-06-20 22:04 王景迁 阅读(20) 评论(0) 推荐(0)

go 基于beego+swagger生成支持直接调用的接口文档
摘要:安装工具 go get github.com/astaxie/beego go get github.com/beego/bee 创建API项目 # 必须在GOPATH下创建项目,否则无法在routers目录下生成commentsRouter_controllers.go,访问URL会报错404 c 阅读全文

posted @ 2025-05-31 23:04 王景迁 阅读(25) 评论(0) 推荐(0)

go 单元测试使用assert
摘要:package test import ( "testing" "github.com/stretchr/testify/assert" ) func TestSomething(t *testing.T) { a := "a" b := "b" // a是expected // b是actual 阅读全文

posted @ 2025-05-31 20:21 王景迁 阅读(27) 评论(0) 推荐(0)

go通过http代理访问
摘要:走代理时会做SNAT,改变源IP和源端口。 package main import ( "fmt" "io" "net/http" "net/url" "time" ) func main() { // 国内代理 proxyStr := "http://39.175.75.144:30001" pr 阅读全文

posted @ 2025-05-12 07:45 王景迁 阅读(26) 评论(0) 推荐(0)

go 实现反向代理
摘要:正向代理和反向代理区别 正向代理代理的是客户端,隐藏了客户端的真实IP,部署在客户端网络中,访问外部资源,例如通过代理访问外网。 反向代理代理的是服务端,隐藏了服务端的真实IP,部署在服务端网络中,提供负载均衡,例如访问nginx。 实现反向代理 package main import ( "log 阅读全文

posted @ 2025-03-29 16:00 王景迁 阅读(75) 评论(0) 推荐(0)

go build报错找不到方法
摘要:main.go package main func main() { Print() } print.go package main func Print() { } 针对单个目录下存放了main.go和引用了方法的go文件时,go build需要使用.或者路径。针对单个目录下只有main.go时, 阅读全文

posted @ 2025-03-17 08:50 王景迁 阅读(24) 评论(0) 推荐(0)

go logrus输出trace_id
摘要:go get github.com/google/uuid go get github.com/sirupsen/logrus 方式1:非并发场景使用hook package main import ( "github.com/sirupsen/logrus" ) type TraceIdHook 阅读全文

posted @ 2025-01-28 15:00 王景迁 阅读(54) 评论(0) 推荐(0)

gorm crud总结
摘要:type Student struct { Id int64 `gorm:"primary_key"` Name string `gorm:"column:name"` Age int `gorm:"column:age"` } func (Student) TableName() string { 阅读全文

posted @ 2024-12-31 21:52 王景迁 阅读(33) 评论(0) 推荐(0)

1 2 3 4 5 下一页

导航