golang笔记


虽然推荐使用

go get -u golang.org/x/sync

但很不幸,国内无法连接golang.org,所以只能曲线救国,借助github.com来安装相关的package

git clone https://github.com/golang/sync ./
# 或者
git clone https://github.com/golang/sync
mv sync $GOPATH/src/golang/x/

读取配置文件

注: Configuration 里元素(Path)必须大写开头

import (
    "encoding/json"
    "flag"
    "fmt"
    "io/ioutil"
)

type Configuration struct {
    Path string
}

const VETSION = "0.1.0"

var (
    config Configuration
)

func init() {
    var (
        conf_file string
        print_ver bool
    )
    flag.StringVar(&conf_file, "c", "etc/config.json", "config file")
    flag.BoolVar(&print_ver, "v", false, "config file")
    flag.Parse()

    raw, err := ioutil.ReadFile(conf_file)
    if err != nil {
        log.Error("config parse fail!")
        os.Exit(1)
    }
    err = json.Unmarshal(raw, &config)
    if err != nil {
        log.Error("config unmarshal fail!")
        os.Exit(1)
    }
    if print_ver {
        fmt.Println("version:", VERSION)
        os.Exit(0)
    }
}

判断文件是否存在

import (
    "os"
)
func file_is_exists(f string) bool {
    _, err := os.Stat(f)
    if os.IsNotExist(err) {
        return false
    }
    return err == nil
}

随机睡眠

import (
    "math/rand"
    "time"
)
func random_sleep(t int) {
    rand.Seed(time.Now().Unix())
    time.Sleep(time.Duration(rand.Intn(t)) * time.Microsecond)
}

字符串

随机的n位字符串

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

func randStringRunes(n int) string {
    rand.Seed(time.Now().UnixNano())
    b := make([]rune, n)
    for i := range b {
        b[i] = letterRunes[rand.Intn(len(letterRunes))]
    }
    return string(b)
}

字符串连接

import (
    "bytes"
)
func string_concat(s ...string) string {
    var buffer bytes.Buffer
    for _, i := range s {
        buffer.WriteString(i)
    }
    return buffer.String()
}

检查元素是否在map中

func check_in_map(f map[string]string, v string) bool {
    if _, ok := f[v]; ok {
        return true
    }
    return false
}

检查元素是否在list中

func check_in_list(f []string, v string) bool {
    for i := range f {
        if f[i] == v {
            return true
        }
    }
    return false
}

执行Linux命令

import (
    "os/exec"
)
func exec_command(command string, args ...string) (error, bool) {
    var (
        err error
    )
    cmd := exec.Command(command, args...)
    cmd.Start()
    done := make(chan error)
    go func() {
        done <- cmd.Wait()
    }()

    select {
    case <-time.After(600 * time.Second):
        if err = cmd.Process.Kill(); err != nil {
            log.Error("failed to kill: %s, error: %s", cmd.Path, err)
        }
        go func() {
            <-done // allow goroutine to exit
        }()
        log.Info("process:%s killed", cmd.Path)
        return err, true
    case err = <-done:
        return err, false
    }
}

迭代文件目录

func file_iter(path string) []string {
    // path must be abs path
    var files []string
    scripts, _ := ioutil.ReadDir(path)
    for _, script := range scripts {
        if script.IsDir() {
            return file_iter(filepath.Join(path, script.Name()))
        }
        files = append(files, filepath.Join(path, script.Name()))
    }
    return files
}

求两列表的差集

func difference(a,b []string) []string {
    // len(a) < len(b), avoid all item of a belong to b
    m := map[string]bool{}
    for _, x := range a {
        m[x] = true
    }
    diff := []string{}
    for _, x := range b {
        if _, ok := m[x]; !ok {
            diff = append(diff, x)
        }
    }
    return diff
}

删除列表中重复的数据

func RemoveDuplicates(elements []string) []string {
   keys := map[string]bool{}
   result := []string{}

   for _, element := range elements {
       if _, value := keys[element]; !value {
           keys[element] = true
           result = append(result, element)
       }
   }
   return result
}

参数校验

是否是IP

func IsValidIP(key string) bool {
    IP := net.ParseIP(key)
    if IP == nil {
        return false
    }

    return true
}

是否是EMAIL

import "regexp"

func IsValidEmail(key string) bool {
    re := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
    if !re.MatchString(key) {
        return false
    }
    if strings.HasSuffix(key, "@upai.com") || strings.HasSuffix(key, "@huaban.com") {
        return true
    }
    return false
}

读取http响应的gz package

func main() {
    resp, _ := HTTPRequest("GET", "http://some-gz-package/package-name", nil)
    defer resp.Body.Close()

    gz, err := gzip.NewReader(resp.Body)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    defer gz.Close()
    scanner := bufio.NewScanner(gz)
    for scanner.Scan() {
        fmt.Println(scanner.Text())
    }
}
Author: honmaple

NO CONTENTS

lin.jiang

风落花语风落天,花落风雨花落田.