摘要:方式1:基本事务(常用) err := db.Transaction(func(tx *gorm.DB) error { // 执行事务操作 return nil }) 方式2:手动事务 // 开始事务 tx := db.Begin() defer func() { if r := recover(
阅读全文
摘要:go有2个方法设置request的context,实现上下文在同一个request中传递。方法1:WithContext func (r *Request) WithContext(ctx context.Context) *Request 浅拷贝旧的request,返回新的request,新老re
阅读全文
摘要:32/64位机器读写占用空间小于或等于32/64位的变量是原子操作,超过时属于非原子操作。 安全的赋值类型基本数据类型:字节型、布尔型、整型、浮点型、字符型其他数据类型:指针、函数 不安全的赋值类型基本数据类型:复数型、字符串其他数据类型:数组、切片、字典、通道、接口、结构体 可以通过atomic.
阅读全文
摘要:服务端 package main import ( "net/http" "time" ) func handler(w http.ResponseWriter, r *http.Request) { f := w.(http.Flusher) for { w.Write([]byte("1\n")
阅读全文
摘要:package main import ( "crypto/tls" "fmt" "log" "net/http" "net/http/httptrace" "time" ) func main() { req, _ := http.NewRequest("GET", "https://www.ba
阅读全文
摘要:场景 条件 执行default 存在default,且无case满足。 阻塞 没有default,且无case满足。 选择单个case执行 如果只有1个case满足,那么直接执行;如果有多个case满足,那么随机选择执行,通过随机来保证公平性。
阅读全文
摘要:package main import "fmt" type student struct { name string age int } func main() { m := make(map[*student]bool) s1 := &student{ name: "a", age: 17, }
阅读全文
摘要:10.11.12.13不真实存在,ping会超时。 使用 http.Client 设置全局超时 package main import ( "net/http" "time" ) func main() { client := &http.Client{ Timeout: 5 * time.Seco
阅读全文
摘要: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
阅读全文
摘要:package main import ( "fmt" ) type Config struct { path string isUseFile bool } type ConfigOption func(config *Config) error func WithPath(path string
阅读全文
摘要:url = schema(http/https) + "//" + host(ip:port/domain:port) + path(/xxx) + "?" + rawQuery(xxx=xxx) package main import ( "fmt" "net/url" ) func parseU
阅读全文
摘要:go context虽然只能保存一对key和value,但是支持any;虽然获取value时支持不断从父context中找,但是无法获取所有kv。 package main import ( "maps" "context" "fmt" ) // context推荐使用自定义结构体类型作为key,与
阅读全文
摘要:curl --proxy-header "代理认证信息" --proxy "代理地址" https://xxx 这里--proxy-header和普通header -H不等价,虽然两者类型相同,但是前者只发送给代理服务端(仅限https场景),后者只发送给服务端。 go Transport实现了ht
阅读全文
摘要:安装工具 go get github.com/astaxie/beego go get github.com/beego/bee 创建API项目 # 必须在GOPATH下创建项目,否则无法在routers目录下生成commentsRouter_controllers.go,访问URL会报错404 c
阅读全文
摘要:package test import ( "testing" "github.com/stretchr/testify/assert" ) func TestSomething(t *testing.T) { a := "a" b := "b" // a是expected // b是actual
阅读全文
摘要:走代理时会做SNAT,改变源IP和源端口。 package main import ( "fmt" "io" "net/http" "net/url" "time" ) func main() { // 国内代理 proxyStr := "http://39.175.75.144:30001" pr
阅读全文
摘要:正向代理和反向代理区别 正向代理代理的是客户端,隐藏了客户端的真实IP,部署在客户端网络中,访问外部资源,例如通过代理访问外网。 反向代理代理的是服务端,隐藏了服务端的真实IP,部署在服务端网络中,提供负载均衡,例如访问nginx。 实现反向代理 package main import ( "log
阅读全文
摘要:main.go package main func main() { Print() } print.go package main func Print() { } 针对单个目录下存放了main.go和引用了方法的go文件时,go build需要使用.或者路径。针对单个目录下只有main.go时,
阅读全文
摘要:go get github.com/google/uuid go get github.com/sirupsen/logrus 方式1:非并发场景使用hook package main import ( "github.com/sirupsen/logrus" ) type TraceIdHook
阅读全文
摘要:type Student struct { Id int64 `gorm:"primary_key"` Name string `gorm:"column:name"` Age int `gorm:"column:age"` } func (Student) TableName() string {
阅读全文