Skip to content

Instantly share code, notes, and snippets.

Avatar

tangx tangx

View GitHub Profile
@tangx
tangx / envvar.go
Created Aug 7, 2020
golang利用反射获取或设置环境变量
View envvar.go
package envvar
import (
"encoding/json"
"fmt"
"os"
"reflect"
"strconv"
"strings"
)
@tangx
tangx / lego-letsencrypt.sh
Last active Jan 13, 2020
lego-letsencrypt.sh
View lego-letsencrypt.sh
#!/bin/bash
#
#
cd $(dirname $0)
DOMAIN="*.example.com"
EMAIL="your@mail.com"
export ALICLOUD_ACCESS_KEY=xxxxxxxxx
export ALICLOUD_SECRET_KEY=yyyyyyyyyyyyyyyyy
@tangx
tangx / gochannel.go
Created Nov 19, 2019
golang 通道
View gochannel.go
package gochannel
import (
"fmt"
"math/rand"
"sync"
"time"
)
// 创建 wg
@tangx
tangx / uint16-posts.go
Created Nov 13, 2019
使用int位数判断特定类型的数据,例如端口
View uint16-posts.go
// "28080:80"
ports := strings.Split(s, ":")
p, err := strconv.ParseUint(ports[0], 10, 16)
if err != nil {
return nil, fmt.Errorf("invalid port %v", ports[0])
}
port = uint16(p)
@tangx
tangx / init.go
Last active Nov 12, 2019
init 初始化
View init.go
// 全局变量
var db *sql.DB
func init() {
NewConn(dsn)
}
// NewConn return a DB conn
func NewConn(dsn string) (err error) {
// sql.Open 不会
@tangx
tangx / time-usage.go
Created Nov 11, 2019
time 的常用用法
View time-usage.go
func Test_time(t *testing.T) {
start := time.Now()
time.Sleep(1 * time.Second)
sec := time.Since(start).Seconds()
fmt.Println(sec)
}
@tangx
tangx / http-client-do.go
Created Nov 11, 2019
发起自定义 http 请求
View http-client-do.go
func reqPost(url string, body io.Reader) ([]byte, error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "go-dnspod (shallwedance@126.com)")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
You can’t perform that action at this time.