Golang net/url.Userinfo type, Password() and Username() functions example
package net/url
Golang net/url.Userinfo type, Password() and Username() functions usage example
package main
import (
"fmt"
"net/url"
)
func main() {
// username and password must be in plain text (see below)
rawURL := "http://username:password@example.com"
parsedURL, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
if parsedURL.User != nil {
user := parsedURL.User
fmt.Println(user.Username())
// otherwise this will print out password minus the "weird" characters
pw, isset := user.Password()
fmt.Println(pw)
fmt.Println("Is set? : ", isset)
} else {
fmt.Println("No username or password found")
}
}
Output :
username
password
Is set? : true
References :
http://golang.org/pkg/net/url/#Userinfo
Advertisement
Something interesting
Tutorials
+12.3k Golang : Display list of countries and ISO codes
+15.2k Golang : Accurate and reliable decimal calculations
+6.4k CodeIgniter : form input set_value cause " to become & quot
+9.9k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+6.2k Golang & Javascript : How to save cropped image to file on server
+14k Golang : convert rune to unicode hexadecimal value and back to rune character
+17.9k Golang : Qt image viewer example
+9.7k Golang : List available AWS regions
+7.9k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+6.3k Golang : How to search a list of records or data structures
+29.9k Golang : Get and Set User-Agent examples