美文网首页
Go语言基本应用

Go语言基本应用

作者: 三分归元币 | 来源:发表于2020-06-11 17:49 被阅读0次

    map

        hash := map[string]int{
            "demo1":1,
            "demo":2,
        }
        
        key := "demo3"
        hash["demo3"] = 5
        fmt.Printf("get key : %s,value : %d\n",key,hash[key])
    

    自行实现链表遍历查询文件

    未来考虑使用协程实现多线程统计,所以定义了通用的struct,其实可以直接拼全路径做string插入链表

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "time"
    )
    
    // 链表节点
    type Node struct {
        data interface{}
        next *Node
    }
    
    var head,tail *Node
    var emptyDir int
    var fileCount int
    func init(){
        head = &Node{nil,nil}
        tail = head
        emptyDir = 0
        fileCount = 0
    }
    // 文件节点
    type FileAdapter struct {
        prefix string
        name string
    }
    
    
    func main(){
        path := "C:"
        root := FileAdapter{
            prefix: path,
            name : "",
        }
        enq(root)
        // 开始时间
        start := time.Now()
        for{
            if isEmpty() {
                break
            }
            node := deq()
            if node == nil {
                break
            }
            cur := node.data.(FileAdapter)
            absPath := cur.prefix+"\\"+cur.name
            files,_ := ioutil.ReadDir(absPath)
            if len(files) == 0{
                emptyDir ++
            }
            for _,f := range files {
                //fmt.Printf("%v \n",absPath+"\\"+f.Name())
                if f.IsDir() {
                    nextPath := FileAdapter{
                        prefix: absPath,
                        name : f.Name(),
                    }
                    enq(nextPath)
                }else {
                    fileCount ++
                }
            }
        }
        cost := time.Since(start)
        fmt.Printf("目录: %v\n",path)
        fmt.Printf("耗时: %v\n",cost)
        fmt.Printf("空文件夹数目 : %v ,文件总数目 : %v\n",emptyDir,fileCount)
    }
    func Test1(){
        fmt.Printf("list is empty :%v\n",isEmpty())
        enq(1)
        enq(2)
        enq(3)
        traversal()
        fmt.Printf("list is empty :%v\n",isEmpty())
        pop := deq()
        fmt.Printf("pop data :%v\n",pop.data)
        pop = deq()
        fmt.Printf("pop data :%v\n",pop.data)
        pop = deq()
        fmt.Printf("pop data :%v\n",pop.data)
        fmt.Printf("list is empty :%v\n",isEmpty())
    }
    
    
    func enq(data interface{}){
        newNode := &Node{data: data,next: nil}
        tail.next = newNode
        tail = newNode
    }
    func deq() *Node{
        if head == tail{
            return nil
        }
        out := head.next
        old := head
        head = out
        old.next = nil
        return out
    }
    func isEmpty()  bool{
        return head == tail
    }
    func traversal(){
        tmp := head.next
        for{
            if tmp == nil{
                break
            }
            fmt.Printf("data : %v\n",tmp.data)
            tmp = tmp.next
        }
    }
    

    使用文件头判断文件类型

    var allows sync.Map
    func init() {
        allows.Store("ffd8ffe000104a464946", "jpg") //JPEG (jpg)
        allows.Store("89504e470d0a1a0a0000", "png") //PNG (png)
        allows.Store("47494638396126026f01", "gif") //GIF (gif)
        allows.Store("424d228c010000000000", "bmp") //16色位图(bmp)
        allows.Store("424d8240090000000000", "bmp") //24位位图(bmp)
        allows.Store("424d8e1b030000000000", "bmp") //256色位图(bmp)
        allows.Store("49492a00227105008037", "tiff")  //TIFF (tif)
    }
    // 获取前面结果字节的二进制
    func bytesToHexString(src []byte) string {
        res := bytes.Buffer{}
        if src == nil || len(src) <= 0 {
            return ""
        }
        temp := make([]byte, 0)
        for _, v := range src {
            sub := v & 0xFF
            hv := hex.EncodeToString(append(temp, sub))
            if len(hv) < 2 {
                res.WriteString(strconv.FormatInt(int64(0), 10))
            }
            res.WriteString(hv)
        }
        return res.String()
    }
    
    // 用文件前面几个字节来判断
    // fSrc: 文件字节流(就用前面几个字节)
    func GetFileType(fSrc []byte) (string,string) {
        var fileType string
        fileCode := bytesToHexString(fSrc)
    
        allows.Range(func(key, value interface{}) bool {
            k := key.(string)
            v := value.(string)
            if strings.HasPrefix(fileCode, strings.ToLower(k)) ||
                strings.HasPrefix(k, strings.ToLower(fileCode)) {
                fileType = v
                return false
            }
            return true
        })
        return fileType,fileCode[0:24]
    }
    
    

    正则表达式提取图片头

    func main()  {
        str := "data:image/webp;base64"
        reg := regexp.MustCompile("(image\\/((jpeg)|(bmp)|(jpg)|(png)|(gif)|(webp)))")
        data := reg.Find([]byte(str))
        fmt.Println(string(data))
    }
    

    解析json

    require (
        github.com/tidwall/gjson v1.6.0
        github.com/tidwall/pretty v1.0.1 // indirect
    )
    
    
        mp := gjson.Get(data, "content.content")
        results := mp.Array()
    

    相关文章

      网友评论

          本文标题:Go语言基本应用

          本文链接:https://www.haomeiwen.com/subject/kiruahtx.html