Go 語言編程執行個體(七)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

本節主要分享:字串數字解析、URL解析、SHA1HASH、BASE64

Go 字串數字解析

從字串中解析出數字,許多情境都需要這麼做。Golang 內建的 strconv 包中提供了相關的函數可供使用。

package mainimport (    "strconv"    "fmt")func main(){    //後面的64是 64bits 的意思    f,_ := strconv.ParseFloat("1.456",64)    fmt.Printf("%T %f\n",f,f)    //0 表示使用進位解析,修改為8或者16就按照8進位,16進位解析    // 64表示結果必須符合 64bit(只能在INT64範圍內)    i,_ := strconv.ParseInt("123",0,64)    fmt.Printf("%T %d\n",i,i)    //ParseInt 可以識別十六進位的數字    d,_:=strconv.ParseInt("0x1c8",0,64)    fmt.Printf("%T %d\n",d,d)    u,_:=strconv.ParseUint("789",0,64)    fmt.Printf("%T %d\n",u,u)    //atoi 基本的十進位解析    k,_:=strconv.Atoi("135")    fmt.Printf("%T %d\n",k,k)    //錯誤    _,e:= strconv.Atoi("test")    fmt.Println(e)}

Go URL解析執行個體

URL提供了一種統一的方法來定位資源。以下是在Go中解析網址的方法,解析樣本URL,包括方案,身分識別驗證資訊,主機,連接埠,路徑,查詢參數和查詢片段。

package mainimport (    "net/url"    "fmt"    "net")func main(){    //樣本網址    s := "postgres://user:pass@host.com:1314/path?k=v#f"    //解析網址,確保無錯    u,err := url.Parse(s)    if err != nil{        panic(err)    }    //訪問 scheme    fmt.Println(u.Scheme)    //User 包含了所有驗證資訊 使用者名稱和密碼都可以單獨來。    fmt.Println(u.User)    fmt.Println(u.User.Username())    p,_:=u.User.Password()    fmt.Println(p)    //Host 包含了 hostname 和 port 可以使用 SplitHostPort解析他們    fmt.Println(u.Host)    host ,port ,_:=net.SplitHostPort(u.Host)    fmt.Println(host)    fmt.Println(port)    //提取路徑和片段    fmt.Println(u.Path)    fmt.Println(u.Fragment)    //要使用K=V格式的字串擷取查詢參數,需要使用RawQuery。也可以解析到map中。    //解析查詢參數映射是從字串到字串的片段。因此索引為0    fmt.Println(u.RawQuery)    m,_:= url.ParseQuery(u.RawQuery)    fmt.Println(m)    fmt.Println(m["k"][0])}

Go SHA1hash執行個體

SHA1散列經常用於計算二進位或文字區塊的短標識。例如,git版本控制系統廣泛使用 SHA1s 來標識版本化的檔案和目錄。以下樣本Go計算SHA1HASH。

package mainimport (    "crypto/sha1"    "fmt")func main(){    s := "only test you"    //new hash    h := sha1.New()    h.Write([]byte(s))    //計算最終散列值,後面nil可以附件到現有的位元組片段    bs := h.Sum(nil)    fmt.Println(s)    fmt.Printf("%x\n",bs)}

Go Base64編碼執行個體

Go 提供對 base64 編碼解碼的內建支援。匯入帶有 b64 名稱的 encoding/base64 軟體包,而不是預設的 base64 。它會節省我們一些空間。Go支援標準和URL相容的 base64,以下是使用標準編碼器進行編碼的方法。

package mainimport (    "encoding/base64"    "fmt")func main(){    data := "abc123!?$*&()'-=@~"    sEnc := base64.StdEncoding.EncodeToString([]byte(data))    fmt.Println(sEnc)    sDec,_:=base64.StdEncoding.DecodeString(sEnc)    fmt.Println(string(sDec))    uEnc := base64.URLEncoding.EncodeToString([]byte(data))    fmt.Println(uEnc)    uDec,_:= base64.URLEncoding.DecodeString(uEnc)    fmt.Println(string(uDec))}

如需進一步討論可以加群:295023494(嵌入式)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.