Go 获取文件 md5 值


package main

import (
    "bufio"
    "crypto/md5"
    "fmt"
    "io"
    "os"

    "v.src.corp.qihoo.net/weblego/lib4go/convert"
)

func fileMdFir(filePath string) string {
    file, _ := os.Open(filePath)
    defer file.Close()
    h := md5.New()
    io.Copy(h, file)
    fileMd1 := convert.U2S(h.Sum(nil))
    return fileMd1
}

func fileMdSec(file string) string {
    f, _ := os.Open(file)
    defer f.Close()
    r := bufio.NewReader(f)
    h := md5.New()
    io.Copy(h, r)
    fileMd2 := convert.U2S(h.Sum(nil))
    return fileMd2
}

func main() {
    fileMd1 := fileMdFir("/tmp/conf/deploy.toml")
    fmt.Println(fileMd1)
    fileMd2 := fileMdSec("/tmp/conf/deploy.toml")
    fmt.Println(fileMd2)
}
阅读全文

Go 字符串替换


package main
 
import (
    "fmt"
    "strings"
)
 
func main() {
    str := "welcome to beijing"
    strF = strings.Replace(str, " ", ",", -1)
    fmt.Println(strF)
    strS = strings.Replace(str, " ", ",", 1)
    fmt.Println(strS)
}
阅读全文

Go convert "type []string" to string


package main

import (
    "fmt"
    "strings"
)

var naiveList string

func main() {
    naiveArray := []string{"a", "b", "c", "d", "e"}
    for _, v := range naiveArray {
        if naiveList == "" {
            naiveList = v
        } else {
            naiveList = strings.Join([]string{naiveList, v}, ",")
        }
    }
    fmt.Println(naiveList)
}
阅读全文

Go 中使用 base64


package main

import (
    "encoding/base64"
    "fmt"
    "log"
)

func main() {
    input := []byte("hello golang base64")

    // base64 encode
    encodeString := base64.StdEncoding.EncodeToString(input)
    fmt.Println(encodeString)

    // base decode
    decodeBytes, err := base64.StdEncoding.DecodeString(encodeString)
    if err != nil {
        log.Fatalln(err)
    }
    fmt.Println(string(decodeBytes))
}
阅读全文

Go cond锁定期唤醒锁


package main

import (
    "fmt"
    "sync"
    "time"
)

var locker = new(sync.Mutex)
var cond = sync.NewCond(locker)

func test(x int) {
    // 获取锁
    cond.L.Lock()
    // 等待通知  暂时阻塞
    cond.Wait()
    fmt.Println(x)
    time.Sleep(time.Second * 1)
    // 释放锁
    cond.L.Unlock()
}
func main() {
    for i := 0; i < 40; i++ {
        go test(i)
    }
    fmt.Println("start all")
    time.Sleep(time.Second * 3)
    fmt.Println("broadcast")
    // 下发一个通知给已经获取锁的goroutine
    cond.Signal()
    time.Sleep(time.Second * 3)
    // 3秒之后 下发一个通知给已经获取锁的goroutine
    cond.Signal()
    time.Sleep(time.Second * 3)
    // 3秒之后 下发广播给所有等待的goroutine
    cond.Broadcast()
    time.Sleep(time.Second * 60)
}
阅读全文

Go 中如何阻塞等待所有 goroutines 都完成


方案一:

package main

import (
    "fmt"
    "runtime"
    "sync"
    "time"
)

// 定义一个同步等待的组
var wg sync.WaitGroup

// 定义一个Printer函数用于并发
func Printer(a int) {
    time.Sleep(2000 * time.Millisecond)
    fmt.Printf("i am %d\n", a)
    defer wg.Done()
}

func main() {
    // 获取cpu个数
    maxProcs := runtime.NumCPU()
    // 限制同时运行的goroutines数量
    runtime.GOMAXPROCS(maxProcs)
    for i := 0; i < 10; i++ {
        //为同步等待组增加一个成员
        wg.Add(1)
        //并发一个goroutine
        go Printer(i)
    }
    // 阻塞等待所有组内成员都执行完毕退栈
    wg.Wait()
    fmt.Println("WE DONE!!!")
}

阅读全文

Go 打印函数执行时间


package main

import (
    "fmt"
    "time"
)

func timeCost(start time.Time) {
    terminal := time.Since(start)
    fmt.Println(terminal)
}

func main() {
    defer timeCost(time.Now())
    fmt.Println("start program")
    time.Sleep(5 * time.Second)
    fmt.Println("finish program")
}
阅读全文