首页 > 其他分享 >0131-Go-URL 解析

0131-Go-URL 解析

时间:2022-11-28 22:35:38浏览次数:77  
标签:0131 URL fmt Println url Go User

环境

  • Time 2022-08-25
  • Go 1.19

前言

说明

参考:https://gobyexample.com/url-parsing

目标

使用 Go 语言的 URL 解析。

示例

package main

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

func main() {

    s := "postgres://user:[email protected]: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)

    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)
    m, _ := url.ParseQuery(u.RawQuery)
    fmt.Println(m)
    fmt.Println(m["k"][0])
}

总结

使用 Go 语言的 URL 解析。

附录

标签:0131,URL,fmt,Println,url,Go,User
From: https://www.cnblogs.com/jiangbo4444/p/16933874.html

相关文章

  • 0132-Go-SHA256
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/sha256-hashes目标使用Go语言的SHA256。示例packagemainimport("crypto/sha256"......
  • 0133-Go-Base64
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/base64-encoding目标使用Go语言的Base64。示例packagemainimport(b64"encoding......
  • 0121-Go-字符串格式化
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/string-formatting目标使用Go语言的字符串格式化。示例packagemainimport("fmt"......
  • 0120-Go-字符串函数
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/string-functions目标使用Go语言的字符串函数。示例packagemainimport("fmt"......
  • 0122-Go-模板字符串
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/text-templates目标使用Go语言的模板字符串。示例packagemainimport("os""t......
  • 0123-Go-正则表达式
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/regular-expressions目标使用Go语言的正则表达式。示例packagemainimport( "bytes" ......
  • 0124-Go-JSON 转换
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/json目标使用Go语言的JSON。简单值packagemainimport("encoding/json""fmt......
  • 0125-Go-XML 转换
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/xml目标使用Go语言的XML。示例packagemainimport("encoding/xml""fmt")......
  • go zap日志库
    https://github.com/labring/sealos/blob/main/pkg/utils/logger/logger.gopackagelogger   import( "fmt" "os" "strings"......
  • go源码学习(一):数据结构-数组
    数组是相同类型元素的集合,在内存中对应一块连续的内存空间。数组类型是通过存储的元素类型以及能够存储的大小两个维度来决定的,一旦声明之后大小就不可更改。初始化go语......