Go http client 发送 get post 请求


引入包

import (
    "net/http"
)

发送 get 请求

res, _ := http.Get(getURL)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))

发送 post 请求

// 将 map 转换成 json jinxingfasong
requestJSON, _ := json.Marshal(requestMap)
req, _ := http.NewRequest("POST", postURL, strings.NewReader(string(requestJSON)))
// 设置请求头
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
    fmt.Println(err)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
阅读全文

Go Redis 库 Redigo


https://github.com/garyburd/redigo/

引入库

import (
    "github.com/garyburd/redigo/redis"
)

查询数据

cli, cerr := redis.Dial("tcp", ip:port, redis.DialPassword(redisPasswd), redis.DialDatabase(1))
if cerr != nil {
    fmt.Println(cerr)
}
defer cli.Close()
// 查询 key
val, err := redis.String(cli.Do("GET", key))
if err != nil {
    fmt.Println(err)
}
return val

写入数据

cli, cerr := redis.Dial("tcp", ip:port, redis.DialPassword(redisPasswd), redis.DialDatabase(1))
if cerr != nil {
    fmt.Println(cerr)
}
defer cli.Close()
_, err := cli.Do("SET", key, value)
if err != nil {
    fmt.Println(err)
}
阅读全文

Go 轻量级 MySQL 库 go-sql-driver


https://github.com/go-sql-driver/mysql

引入库

import (
    "database/sql"

    _ "github.com/go-sql-driver/mysql"
)

查询数据

// 建立 MySQL 链接
db, err := sql.Open("mysql", "user:passwd@tcp(ip:port)/db")
if err != nil {
    fmt.Println(err)
}
defer db.Close()
// 执行 SQL
rows, rerr := db.Query(sqlInfo)
if rerr != nil {
    fmt.Println(rerr)
}
defer rows.Close()
// 获取 SQL 数据
for rows.Next() {
    var returnData string
    err := rows.Scan(&returnData)
    if err != nil {
        fmt.Println(err)
    }
    return returnData
}
return returnData

写入/更新数据

// 建立 MySQL 链接
db, err := sql.Open("mysql", "user:passwd@tcp(ip:port)/db")
if err != nil {
    fmt.Println(err)
}
defer db.Close()
// 执行 SQL
db.Exec(sqlInfo)
阅读全文

Go 读写 excel


读 excel

package main

import (
    "fmt"

    "github.com/tealeg/xlsx"
)

func main() {
    excelFileName := "test.xls"
    xlFile, _ := xlsx.OpenFile(excelFileName)
    for _, sheet := range xlFile.Sheets {
        for _, row := range sheet.Rows {
            for _, cell := range row.Cells {
                fmt.Println(cell.String())
            }
        }
    }
}

写 excel

package main

import (
    "encoding/csv"
    "os"
)

func main() {
    f, err := os.Create("test.xls")
    if err != nil {
        panic(err)
    }
    defer f.Close()
    // 写入 utf-8 编码
    f.WriteString("\xEF\xBB\xBF")
    w := csv.NewWriter(f)
    w.Write([]string{"编号", "姓名", "年龄"})
    w.Write([]string{"1", "张三", "23"})
    w.Write([]string{"2", "李四", "24"})
    w.Write([]string{"3", "王五", "25"})
    w.Write([]string{"4", "赵六", "26"})
    w.Flush()
}
阅读全文

使用 Visual Studio Code 开发 Go


设置 Windows 环境变量:

GOPATH = E:\Soft\GoPath

GOROOT = C:\Program Files\Go

Path = %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Python\;C:\Program Files\Python\Scripts;C:\Program Files\Go\bin;

阅读全文