Updated April 20, 2023
Introduction to Golang URL
In Golang URL is very important to package it used to fetch the data from servers. Simply understand if you are working on any application and you want to fetch data for this application from any external place or server then we can use the URL. It allows us to handle various parameters of any URL, like a port of URL, search string in URL, etc. URL can contain various methods which allow it to deal with URLs attributes and for modifications, for example, if we have an URL like www.exmple.com:3000 and so 3000 is the port of the URL and with the help of the URL package function we can access the port number, in the same way, there is the function to check if the URL format ios valid or not.
Syntax
In the below we have written a very simple syntax for the URL, we can explain each attribute of the syntax with below points,
URL, error := url.inbuilt-function-name("url")
URL: This contains the URL name and some basic details of the URL. We can give any name to it. It is like any variable.
error: This is the error section, in case the URL is wrong or any exception will be there in that case the URL will be a return error and that error will be captured in the error section.
inbuilt-function-name: As we discuss that there are many functions in the URL package to deal with URLs like Parse, Path, Rawpath, string() all these functions we can use for different purposes.
How does URL work in the Go language?
Before understanding the working of the url we need to understand the basic uses, we use the url package of the Go language to deal with us. You know when we hit any url it can contain many attributes like it can have some port, it can have some search, it can have some path, etc, so to manipulate and work with all those things we use the URL. Let us understand the working of the URL in the go language.
To use the url we need to import the o package called net/url. We can pass the url as the argument to the functions of the url.
For example, if we have an url and we want to check the port of the given url by calling the function of the url like Port() where u =url.Parse(“www.xyz.com”).
In the same way, we can check the url if the format of the url is correct or not.
Examples to Implement Golang URL
In the below we have given some of the important examples of the go url, we are fetching the port of the URL, we checked if the url is absolute or not which returns true and false and we have given some examples to fetch the path, search string, etc. To run our test the given examples we can create a file with name url.go and paste the example of the code on the file and you can run the command go run url.go and we can get the output.
Example #1
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"net/url"
"log"
)
func ExampleURL() {
URL, error := url.Parse("http://bing.com/search?q=php")
fmt.Println("Url before modification is",URL)
if error != nil {
log.Fatal("An error occurs while handling url" ,error)
}
URL.Scheme = "https"
URL.Host = "test.com"
query := URL.Query()
query.Set("The query", "go language")
URL.RawQuery = query.Encode()
fmt.Println("The url after modification is",URL)
}
func main() {
ExampleURL()
}
Output:
Example #2
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"net/url"
"log"
)
func ExampleURL() {
URL, error := url.Parse("http://bing.com/good%2bad")
fmt.Println("Url before modification is",URL)
if error != nil {
log.Fatal("An error occurs while handling url",error)
}
fmt.Println("The URL path is",URL.Path)
fmt.Println("The URL raw path is",URL.RawPath)
fmt.Println("The URL is ",URL.String())
}
func main() {
ExampleURL()
}
Output:
Example #3
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"net/url"
"log"
)
func ExampleURL() {
URL, error := url.Parse("../../..//search?q=php")
fmt.Println("Url before modification is",URL)
if error != nil {
log.Fatal("An error occurs while handling url" ,error)
}
baseURL, err := url.Parse("http://example.com/directory/")
if err != nil {
log.Fatal("An error occurs while handling url",err)
}
fmt.Println(baseURL.ResolveReference(URL))
}
func main() {
ExampleURL()
}
Output:
Example #4
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"net/url"
"log"
)
func ExampleURL() {
URL, error := url.Parse(`x=1&y=2&y=3;z`)
if error != nil {
log.Fatal("An error occurs while handling url" ,error)
}
fmt.Println("The Url is",URL)
}
func main() {
ExampleURL()
}
Output:
Example #5
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"net/url"
"log"
)
func ExampleURL() {
URL, error := url.Parse("http://example.com/Here path with space")
if error != nil {
log.Fatal("An error occurs while handling url" ,error)
}
fmt.Println("The Url is",URL)
}
func main() {
ExampleURL()
}
Output:
Example #6
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"net/url"
)
func ExampleURL() {
URL:= url.URL{Host: "example.com", Path: "test"}
fmt.Println("The Url is",URL.IsAbs())
}
func main() {
ExampleURL()
}
Output:
Example #7
Please see the below example along with the screen of the output.
Code:
package main
import (
"fmt"
"net/url"
"log"
)
func ExampleURL() {
URL1, error := url.Parse("https://example.org")
fmt.Println("URL1 before modification is",URL1)
if error != nil {
log.Fatal("An error occurs while handling url" ,error)
}
URL2, err := url.Parse("https://example.org:8080")
if err != nil {
log.Fatal("An error occurs while handling url",URL2)
}
fmt.Println("URL2 before modification is",URL2)
fmt.Println("URL2 Port number is",URL2.Port())
}
func main() {
ExampleURL()
}
Output:
Conclusion
From this tutorial, we learned the basic concept of the go url and we learned about the syntax of the url. We focus on the working of the url along with some of its important uses which show the important functions of the url and also the main uses of the url.
Recommended Articles
This is a guide to Golang URL. Here we discuss an introduction to Golang URL, syntax, how does it work with programming examples. You can also go through our other related articles to learn more –