当前位置:   article > 正文

1.4 Go学习之正则&Json处理&时间&工具类_go正则匹配json字符串

go正则匹配json字符串

正则表达式

package main

import (
    "bytes"
    "fmt"
    "regexp"
)

func main() {

    match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
    fmt.Println(match)  	//true

//Above we used a string pattern directly
//but for other regexp tasks you’ll need to Compile an optimized Regexp struct
    r, _ := regexp.Compile("p([a-z]+)ch")  

//Many methods are available on these structs
    fmt.Println(r.MatchString("peach"))   //true

    fmt.Println(r.FindString("peach punch"))  //peach

//返回第一个匹配的开始索引和结束索引
    fmt.Println(r.FindStringIndex("peach punch")) //[0 5]

//return information for both p([a-z]+)ch and ([a-z]+)
//既完全匹配又部分匹配
    fmt.Println(r.FindStringSubmatch("peach punch")) //[peach ea]

    fmt.Println(r.FindStringSubmatchIndex("peach punch")) //[0 5 1 3]

//返回所有匹配,不仅仅是第一个匹配
    fmt.Println(r.FindAllString("peach punch pinch", -1)) //[peach punch pinch]

    fmt.Println(r.FindAllStringSubmatchIndex(
        "peach punch pinch", -1))  //[[0 5 1 3] [6 11 7 9] [12 17 13 15]]

//2限制match的number
    fmt.Println(r.FindAllString("peach punch pinch", 2)) //[peach punch]

//形参为byte[]类型
//provide []byte arguments and drop String from the function name
    fmt.Println(r.Match([]byte("peach"))) //true

//When creating global variables with regular expressions you can use the MustCompile 
//MustCompile panics instead of returning an error
    r = regexp.MustCompile("p([a-z]+)ch")
    fmt.Println(r)  //p([a-z]+)ch

//正则被用于替换string中的匹配值
//replace subsets of strings with other values
    fmt.Println(r.ReplaceAllString("a peach", "<fruit>")) //a <fruit>

//transform matched text with a given function
    in := []byte("a peach")
    out := r.ReplaceAllFunc(in, bytes.ToUpper)
    fmt.Println(string(out))   //a PEACH
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

JSON(tocheck)

  • Question:

    • map[string]int{"apple": 5, "lettuce": 7}
      
      • 1
package main

import (
    "encoding/json"
    "fmt"
    "os"
)

//Only exported fields will be encoded/decoded in JSON. 
//Fields must start with capital letters to be exported.
type response1 struct {
    Page   int
    Fruits []string
}

type response2 struct {
    Page   int      `json:"page"`
    Fruits []string `json:"fruits"`
}

func main() {
//encoding basic data types to JSON strings
    bolB, _ := json.Marshal(true)
    fmt.Println(string(bolB))

    intB, _ := json.Marshal(1)
    fmt.Println(string(intB))

    fltB, _ := json.Marshal(2.34)
    fmt.Println(string(fltB))

    strB, _ := json.Marshal("gopher")
    fmt.Println(string(strB))

    slcD := []string{"apple", "peach", "pear"}
    slcB, _ := json.Marshal(slcD)
    fmt.Println(string(slcB))

    mapD := map[string]int{"apple": 5, "lettuce": 7}
    mapB, _ := json.Marshal(mapD)
    fmt.Println(string(mapB))  //{"apple":5,"lettuce":7}

//encode自定义数据类型
//将只是encode大写的fields,且将这些fields的名字作为json的key name
//It will only include exported fields in the encoded output 
//and will by default use those names as the JSON keys.
    res1D := &response1{
        Page:   1,
        Fruits: []string{"apple", "peach", "pear"}}
    res1B, _ := json.Marshal(res1D)
    fmt.Println(string(res1B)) //{"Page":1,"Fruits":["apple","peach","pear"]}

//在结构体定义中使用 tag来自定义key的名字
    res2D := &response2{
        Page:   1,
        Fruits: []string{"apple", "peach", "pear"}}
    res2B, _ := json.Marshal(res2D)
    fmt.Println(string(res2B)) //{"page":1,"fruits":["apple","peach","pear"]}

//下例为json到数据类型
    byt := []byte(`{"num":6.13,"strs":["a","b"]}`)

//decoded map存放转换的映射数据,key为string,value为对应的数据类型
    var dat map[string]interface{}

    if err := json.Unmarshal(byt, &dat); err != nil {
        panic(err)
    }
    fmt.Println(dat) //map[num:6.13 strs:[a b]]

    num := dat["num"].(float64) //convert the value in num to the expected float64 type
    fmt.Println(num) //6.13

    strs := dat["strs"].([]interface{}) 
    //Accessing nested data requires a series of conversions.
    str1 := strs[0].(string)
    fmt.Println(str1) //a

//直接将json转换为自定义数据类型
//has the advantages of adding additional type-safety to our programs 
//and eliminating the need for type assertions
    str := `{"page": 1, "fruits": ["apple", "peach"]}`
    res := response2{}
    json.Unmarshal([]byte(str), &res)
    fmt.Println(res) //{1 [apple peach]}
    fmt.Println(res.Fruits[0]) //apple
//上述例子我们将bytes和strings作为数据和在标准输出的json表达的intermediates
//used bytes and strings as intermediates 
//between the data and JSON representation on standard out
    enc := json.NewEncoder(os.Stdout)
    d := map[string]int{"apple": 5, "lettuce": 7}
//We can also stream JSON encodings directly to os.Writers like os.Stdout 
//or even HTTP response bodies.
    enc.Encode(d)  //{"apple":5,"lettuce":7}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

XML

package main

import (
    "encoding/xml"
    "fmt"
)

	//	field tags contain directives for the encoder and decoder
type Plant struct {
    XMLName xml.Name `xml:"plant"`
    //xmlName这个field表明这个结构体的XML元素名字
    //attr表明ID这个field是XML的属性而不是内置的元素
    //<plant id="27">
    Id      int      `xml:"id,attr"`
    Name    string   `xml:"name"`
    Origin  []string `xml:"origin"`
}

func (p Plant) String() string {
    return fmt.Sprintf("Plant id=%v, name=%v, origin=%v",
        p.Id, p.Name, p.Origin)
}

func main() {
    coffee := &Plant{Id: 27, Name: "Coffee"}
    coffee.Origin = []string{"Ethiopia", "Brazil"}

//生成易读的XML
    out, _ := xml.MarshalIndent(coffee, " ", "  ")
    fmt.Println(string(out))

//手动添加header
    fmt.Println(xml.Header + string(out))

    var p Plant
 //Use Unmarhshal to parse a stream of bytes with XML into a data structure
 //若XML格式不对或者无法完成映射,则返回error值
    if err := xml.Unmarshal(out, &p); err != nil {
        panic(err)
    }
    fmt.Println(p)

    tomato := &Plant{Id: 81, Name: "Tomato"}
    tomato.Origin = []string{"Mexico", "California"}

    type Nesting struct {
        XMLName xml.Name `xml:"nesting"`
    //parent>child>plant
    //tells the encoder to nest all plants under <parent><child>
        Plants  []*Plant `xml:"parent>child>plant"`
    }

    nesting := &Nesting{}
    nesting.Plants = []*Plant{coffee, tomato}

    out, _ = xml.MarshalIndent(nesting, " ", "  ")
    fmt.Println(string(out))
}

<plant id="27">
   <name>Coffee</name>
   <origin>Ethiopia</origin>
   <origin>Brazil</origin>
 </plant>
<?xml version="1.0" encoding="UTF-8"?>
 <plant id="27">
   <name>Coffee</name>
   <origin>Ethiopia</origin>
   <origin>Brazil</origin>
 </plant>
Plant id=27, name=Coffee, origin=[Ethiopia Brazil]
 <nesting>
   <parent>
     <child>
       <plant id="27">
         <name>Coffee</name>
         <origin>Ethiopia</origin>
         <origin>Brazil</origin>
       </plant>
       <plant id="81">
         <name>Tomato</name>
         <origin>Mexico</origin>
         <origin>California</origin>
       </plant>
     </child>
   </parent>
 </nesting>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

Time

package main

import (
    "fmt"
    "time"
)

func main() {
    p := fmt.Println

    now := time.Now()
    p(now)
//build a time struct by providing the year, month, day, etc. 
//Times are always associated with a Location, i.e. time zone
    then := time.Date(
        2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
    p(then)

    p(then.Year())
    p(then.Month())
    p(then.Day())
    p(then.Hour())
    p(then.Minute())
    p(then.Second())
    p(then.Nanosecond())
    p(then.Location())

    p(then.Weekday())

    p(then.Before(now))
    p(then.After(now))
    //compare the time as the second
    p(then.Equal(now))

//返回两个时间点的interval
    diff := now.Sub(then)
    p(diff)
//不同计量单位
    p(diff.Hours())
    p(diff.Minutes())
    p(diff.Seconds())
    p(diff.Nanoseconds())

    //advance time
    p(then.Add(diff))
    //move backwards
    p(then.Add(-diff))
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

Epoch

package main

import (
    "fmt"
    "time"
)

func main() {

    now := time.Now()
    //基于秒
    secs := now.Unix()
    //基于纳秒
    nanos := now.UnixNano()
    fmt.Println(now)
   //没有UnixMillis,自己手动算
    millis := nanos / 1000000
    fmt.Println(secs)
    fmt.Println(millis)
    fmt.Println(nanos)
   //从纳秒等转化为time表示
    fmt.Println(time.Unix(secs, 0))
    fmt.Println(time.Unix(0, nanos))
}
2012-10-31 16:13:58.292387 +0000 UTC
1351700038
1351700038292
1351700038292387000
2012-10-31 16:13:58 +0000 UTC
2012-10-31 16:13:58.292387 +0000 UTC
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

Time Formatting & Parsing

package main

import (
    "fmt"
    "time"
)

func main() {
    p := fmt.Println

    t := time.Now()
    p(t.Format(time.RFC3339)) //2014-04-15T18:00:15-07:00

    t1, e := time.Parse(
        time.RFC3339,
        "2012-11-01T22:08:41+00:00")  //2012-11-01 22:08:41 +0000 +0000
    p(t1)

   //自定义格式化格式,相当于自己给个example
    p(t.Format("3:04PM"))
    p(t.Format("Mon Jan _2 15:04:05 2006"))
    p(t.Format("2006-01-02T15:04:05.999999-07:00"))
    /*
    6:00PM
		Tue Apr 15 18:00:15 2014
		2014-04-15T18:00:15.161182-07:00
    */
    form := "3 04 PM"
    t2, e := time.Parse(form, "8 41 PM")
    p(t2)
    //0000-01-01 20:41:00 +0000 UTC

//基于string的format方法来格式化
    fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
        t.Year(), t.Month(), t.Day(),
        t.Hour(), t.Minute(), t.Second())
   //2014-04-15T18:00:15-00:00
   
   //若输入有错,Parse方法会返回err
    ansic := "Mon Jan _2 15:04:05 2006"
    _, e = time.Parse(ansic, "8:41PM") 
    //parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": ...
    p(e)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

随机数

  • 提供伪随机数的生成
package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
//rand.Intn returns a random int n, 0 <= n < 100
    fmt.Print(rand.Intn(100), ",")
    fmt.Print(rand.Intn(100))
    fmt.Println()
//rand.Float64 returns a float64 f, 0.0 <= f < 1.0
    fmt.Println(rand.Float64())

    fmt.Print((rand.Float64()*5)+5, ",")
    fmt.Print((rand.Float64() * 5) + 5)
    fmt.Println()
//The default number generator is deterministic
//so it’ll produce the same sequence of numbers each time by default.
//入口参数为随机数序列的seed
    s1 := rand.NewSource(time.Now().UnixNano())
    r1 := rand.New(s1)
//Call the resulting rand.Rand just like the functions on the rand package.
    fmt.Print(r1.Intn(100), ",")
    fmt.Print(r1.Intn(100))
    fmt.Println()
0,28
//If you seed a source with the same number, it produces the same sequence of random numbers.
    s2 := rand.NewSource(42)
    r2 := rand.New(s2)
    fmt.Print(r2.Intn(100), ",")
    fmt.Print(r2.Intn(100))
    fmt.Println()
    
    s3 := rand.NewSource(42)
    r3 := rand.New(s3)
    fmt.Print(r3.Intn(100), ",")
    fmt.Print(r3.Intn(100))
    
5,87
5,87
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

Number parsing

  • 基于strconv包
package main

import (
    "fmt"
    "strconv"
)

func main() {

    f, _ := strconv.ParseFloat("1.234", 64)
    fmt.Println(f)

//the 0 means infer the base from the string. 64 requires that the result fit in 64 bits
i, _ := strconv.ParseInt("123", 0, 64)
    fmt.Println(i)

//能解析十六进制数字
    d, _ := strconv.ParseInt("0x1c8", 0, 64)
    fmt.Println(d) //456

    u, _ := strconv.ParseUint("789", 0, 64)
    fmt.Println(u)

//用于十进制的数字转换
    k, _ := strconv.Atoi("135")
    fmt.Println(k)

//错误input则报错
    _, e := strconv.Atoi("wat")
    fmt.Println(e)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

URL parsing

package main

import (
    "fmt"
    "net"
    "net/url"
)

func main() {

//包括scheme, authentication info, host, port, path, query params, and query fragment
    s := "postgres://user:pass@host.com:5432/path?k=v#f"

    u, err := url.Parse(s)
    if err != nil {
        panic(err)
    }

    fmt.Println(u.Scheme)

    fmt.Println(u.User)
    fmt.Println(u.User.Username())
    p, _ := u.User.Password()
    fmt.Println(p)

//Host包括端口和hostname
    fmt.Println(u.Host)
    host, port, _ := net.SplitHostPort(u.Host)
    fmt.Println(host)
    fmt.Println(port)

    fmt.Println(u.Path)
    fmt.Println(u.Fragment)

    fmt.Println(u.RawQuery)
    //将query参数转换为map,key为strings,value为strings的slice
    //所以 index into [0] if you only want the first value
    m, _ := url.ParseQuery(u.RawQuery)
    fmt.Println(m)
    fmt.Println(m["k"][0])
}

postgres
user:pass
user
pass
host.com:5432
host.com
5432
/path
f
k=v
map[k:[v]]
v
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

SHA1 Hashes

  • compute short identities for binary or text blob
  • 例如Git使用该值我作为版本控制的标识符
  • The pattern for generating a hash is sha1.New(), sha1.Write(bytes), then sha1.Sum([]byte{})
  • 基于上述pattern,可以基于其他算法生成hash值,例如计算MD5哈希,则import crypto/md5然后usemd5.New()
  • Hash长度影响加密性
package main

import (
    "crypto/sha1"
    "fmt"
)

func main() {
    s := "sha1 this string"

    h := sha1.New()
//If you have a string s, use []byte(s) to coerce it to bytes
    h.Write([]byte(s))
	//获取最终哈希值(byte slice类型)
	//This gets the finalized hash result as a byte slice. 
	//The argument to Sum can be used to append to an existing byte slice: it usually isn’t needed
    bs := h.Sum(nil)

    fmt.Println(s)
    //SHA1 values are often printed in hex
    、、for example in git commits. Use the %x format verb to convert a hash results to a hex string.
    fmt.Printf("%x\n", bs)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Base64 Encoding

  • Go支持标准的和URL兼容的base64
package main

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

func main() {

    data := "abc123!?$*&()'-=@~"

    sEnc := b64.StdEncoding.EncodeToString([]byte(data))
    fmt.Println(sEnc)

    sDec, _ := b64.StdEncoding.DecodeString(sEnc)
    fmt.Println(string(sDec))
    fmt.Println()

    uEnc := b64.URLEncoding.EncodeToString([]byte(data))
    fmt.Println(uEnc)
    uDec, _ := b64.URLEncoding.DecodeString(uEnc)
    fmt.Println(string(uDec))
}

两种编码方式的末尾字符不同
YWJjMTIzIT8kKiYoKSctPUB+
abc123!?$*&()'-=@~
YWJjMTIzIT8kKiYoKSctPUB-
abc123!?$*&()'-=@~

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/572101
推荐阅读
相关标签
  

闽ICP备14008679号