分类「golang」的所有文章

golang 解析INI配置文件

golang 解析INI配置文件

package main import ( "fmt" "github.com/go-ini/ini" "os" ) func main() { cfg, err := ini.Load("src/test/my.ini") if err != nil { fmt.Printf("Fail to read file: %v", err) os.Exit(1) } // 典型读取操作,默认分区可以使用空字...

golang 加权轮询(Weighted Round-Robin Scheduling)实现

golang 加权轮询(Weighted Round-Robin Scheduling)实现

package main import ( "fmt" "time" ) var slaveDns = map[int]map[string]interface{}{ 0: {"connectstring": "root@tcp(172.16.0.164:3306)/shiqu_tools?charset=utf8", "weight": 8}, 1: {"connectstring": "root@tcp(172.16.0.165:3306)/shiqu_too...

go 语言string、int、int64互相转换

go 语言string、int、int64互相转换

#string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt(string, 10, 64) #int到string string:=strconv.Itoa(int) #int64到string string:=strconv.FormatInt(int64,10)

golang 获取目录下的路径、文件名、目录名

golang 获取目录下的路径、文件名、目录名

package main import ( "os" "io/ioutil" "fmt" "log" "path/filepath" ) func main() { pwd,_ := os.Getwd() // 获取目录下的文件和目录名 getFileAndDirName(pwd) // 获取当录下的文件和目录的路径 getFileAndDirPath(pwd) // 递归获...

golang 比较slice/struct/map 是否相等

golang 比较slice/struct/map 是否相等

package main import ( "fmt" "reflect" ) type A struct { s string } func main() { a1 := A{s: "abc"} a2 := A{s: "abc"} if reflect.DeepEqual(a1, a2) { fmt.Println(a1, "==", a2) } b1 := []int{1, 2} b2 := []int{1, 2} if...

golang base64的编码和解码

golang base64的编码和解码

demo: package main import ( "encoding/base64" "fmt" ) func main() { /* StdEncoding: 常规编码 URLEncoding: URL safe 编码 RawStdEncoding: 常规编码,末尾不补 = RawURLEncoding: URL safe 编码,末尾不补 = */ msg := []byte("Hello world. 哈喽 沃尔德") ...

golang 字符串,gizp 压缩、解压缩

golang 字符串,gizp 压缩、解压缩

demo package main import ( "bytes" "compress/gzip" "encoding/binary" "io/ioutil" "fmt" ) func main() { // 压缩 c, _ := createGzip([]byte("zdwork")) fmt.Println(c.String()) // 解压缩 p, _ := parseGzip(c.Bytes()) ...

golang 捕获进程信号

golang 捕获进程信号

注意:SIGKILL信号是不能被捕获的 demo: package main import ( "fmt" "os" "os/signal" "syscall" ) func main() { ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR2, syscall.SIGKILL) for { sig := &l...

gin 使用笔记

gin 使用笔记

基于httprouter开发的web框架 https://github.com/julienschmidt/httprouter gin 项目地址 https://github.com/gin-gonic/gin 1、快速启动一个web应用 package main import "github.com/gin-gonic/gin" func testHandle(c *gin.Context) { c.Request.Cookie("") c.JSON(200, gin.H{ "message&...

golang 超时控制实现

golang 超时控制实现

demo 1: // _Timeouts_ are important for programs that connect to // external resources or that otherwise need to bound // execution time. Implementing timeouts in Go is easy and // elegant thanks to channels and `select`. package main import "time" import "fmt" func main() { // For our e...