美文网首页
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语言基本应用

    map 自行实现链表遍历查询文件 未来考虑使用协程实现多线程统计,所以定义了通用的struct,其实可以直接拼全路...

  • (四)go语言函数&参数传递

    go语言函数 基本语法 例子 go语言参数传递

  • 笨办法学golang(三)

    这是Go语言学习笔记第三篇。 Go语言学习笔记参考书籍「Go语言圣经」以及Go官方标准库 Go语言基本类型主要有布...

  • 跟我一起学习GO语言003

    接上一节继续学习Go基本语法与使用-字符串应用 通过Go语言内建函数获取切片、字符串、通道等长度。 例如-01: ...

  • go环境以及kubernetes的go-client介绍

    1、go语言介绍: (1)Go语言是谷歌2009发布的第二款开源编程语言。 (2)Go语言专门针对多处理器系统应用...

  • docker学习

    一 基本介绍 Docker是一个开源的应用容器引擎,基于Go语言开发Docker可以让开发者打包他们的应用与依赖到...

  • Go Web编程.epub

    【下载地址】 《Go Web编程》介绍如何用Go语言进行Web应用的开发,将Go语言的特性与Web开发实战组合到一...

  • 【Docker系列01】Docker Mac安装教程

    1、基本介绍: Docker 是一个开源的应用容器引擎,基于Go语言并遵从Apache2.0协议开源。 Docke...

  • Go 语言 Unit Testing 单元测试

    关于 Go 的基本语法,参见:半天时间 Go 语言的基本实践 单元测试 Go 中提供了 testing 这个 pa...

  • [docker]基于Centos7安装Docker

    Docker 是一个开源的应用容器引擎,基于 Go 语言[https://www.runoob.com/go/go...

网友评论

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

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