Skip to main content
St Louis

Back to all posts

How to Extract Base Url Using Golang?

Published on
4 min read
How to Extract Base Url Using Golang? image

Best Golang Programming Books to Buy in October 2025

1 Go Programming Language, The (Addison-Wesley Professional Computing Series)

Go Programming Language, The (Addison-Wesley Professional Computing Series)

BUY & SAVE
$28.29 $49.99
Save 43%
Go Programming Language, The (Addison-Wesley Professional Computing Series)
2 Learning Go: An Idiomatic Approach to Real-World Go Programming

Learning Go: An Idiomatic Approach to Real-World Go Programming

BUY & SAVE
$45.95 $65.99
Save 30%
Learning Go: An Idiomatic Approach to Real-World Go Programming
3 System Programming Essentials with Go: System calls, networking, efficiency, and security practices with practical projects in Golang

System Programming Essentials with Go: System calls, networking, efficiency, and security practices with practical projects in Golang

BUY & SAVE
$27.36 $41.99
Save 35%
System Programming Essentials with Go: System calls, networking, efficiency, and security practices with practical projects in Golang
4 Mastering Go: Leverage Go's expertise for advanced utilities, empowering you to develop professional software

Mastering Go: Leverage Go's expertise for advanced utilities, empowering you to develop professional software

BUY & SAVE
$29.27 $54.99
Save 47%
Mastering Go: Leverage Go's expertise for advanced utilities, empowering you to develop professional software
5 Go Programming - From Beginner to Professional: Learn everything you need to build modern software using Go

Go Programming - From Beginner to Professional: Learn everything you need to build modern software using Go

BUY & SAVE
$39.99
Go Programming - From Beginner to Professional: Learn everything you need to build modern software using Go
6 Pro Go: The Complete Guide to Programming Reliable and Efficient Software Using Golang

Pro Go: The Complete Guide to Programming Reliable and Efficient Software Using Golang

BUY & SAVE
$31.03 $69.99
Save 56%
Pro Go: The Complete Guide to Programming Reliable and Efficient Software Using Golang
7 Golang Programming For Beginners; An Easy Guide to Learning Golang: A Beginner's Step-by-Step Approach

Golang Programming For Beginners; An Easy Guide to Learning Golang: A Beginner's Step-by-Step Approach

BUY & SAVE
$11.99
Golang Programming For Beginners; An Easy Guide to Learning Golang: A Beginner's Step-by-Step Approach
+
ONE MORE?

To extract base URL using Golang, you can use the url package to parse the URL and then retrieve the base URL. Here is a simple example code snippet to extract base URL from a given URL:

package main

import ( "fmt" "net/url" )

func main() { fullUrl := "https://www.example.com/path/to/something" parsedUrl, err := url.Parse(fullUrl) if err != nil { fmt.Println("Error parsing URL:", err) return }

baseUrl := fmt.Sprintf("%s://%s", parsedUrl.Scheme, parsedUrl.Host)
fmt.Println("Base URL:", baseUrl)

}

In this code snippet, we first parse the full URL using url.Parse function to get the parsedUrl object. Then, we extract the scheme and host from the parsed URL to form the base URL. Finally, we print out the base URL.

You can adjust this code snippet as needed to suit your specific requirements for extracting the base URL using Golang.

What is the function to extract base domain in Golang?

To extract the base domain in Golang, you can use a package like "url" and the "hostname" function from that package. Here is an example code snippet that demonstrates how to extract the base domain in Golang:

package main

import ( "fmt" "net/url" )

func extractBaseDomain(u *url.URL) string { parts := strings.Split(u.Hostname(), ".") if len(parts) < 2 { return "" }

return parts\[len(parts)-2\] + "." + parts\[len(parts)-1\]

}

func main() { u, err := url.Parse("https://www.example.com/path/to/page") if err != nil { fmt.Println("Error parsing URL: ", err) return }

baseDomain := extractBaseDomain(u)
fmt.Println("Base Domain: ", baseDomain)

}

This code snippet defines a function extractBaseDomain that takes a parsed URL and extracts the base domain from it. It splits the hostname into parts using "." as a separator and then returns the last two parts joined by a dot. The main function demonstrates how to use this function by parsing a sample URL and printing the extracted base domain.

What is the code snippet to extract base URL from a URL string in Golang?

Here is a code snippet to extract the base URL from a URL string in Golang:

package main

import ( "fmt" "net/url" )

func main() { u, err := url.Parse("https://www.example.com/path/to/page") if err != nil { panic(err) }

baseURL := fmt.Sprintf("%s://%s", u.Scheme, u.Host)
fmt.Println(baseURL)

}

This code snippet uses the net/url package in Golang to parse the input URL string and extract the scheme and host components to construct the base URL. The base URL in this case would be https://www.example.com.

What is the function to extract base URL with only the root domain in Golang?

To extract the base URL with only the root domain in Golang, you can use the following function:

package main

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

func ExtractRootDomain(baseURL string) (string, error) { parsedURL, err := url.Parse(baseURL) if err != nil { return "", err }

parts := strings.Split(parsedURL.Hostname(), ".")
if len(parts) < 2 {
	return "", fmt.Errorf("Invalid domain format")
}

rootDomain := parts\[len(parts)-2\] + "." + parts\[len(parts)-1\]
return rootDomain, nil

}

func main() { baseURL := "https://www.example.com/some/page" rootDomain, err := ExtractRootDomain(baseURL) if err != nil { fmt.Println("Error:", err) return }

fmt.Println("Root domain:", rootDomain)

}

In this function, the ExtractRootDomain function takes a URL string as input, parses it using the url.Parse function, extracts the hostname from the URL, splits the hostname into parts based on the "." delimiter, and then constructs and returns the root domain using the last two parts of the split hostname.

You can use this function to extract the base URL with only the root domain in Golang.

How to extract base URL with subdomain in Golang?

You can extract the base URL with subdomain in Golang by parsing the URL and extracting the subdomain and domain separately. Here's an example code snippet showing how to extract the base URL with subdomain in Golang:

package main

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

func main() { // URL to parse u := "http://subdomain.example.com/path/to/page"

// Parse the URL
parsedURL, err := url.Parse(u)
if err != nil {
	fmt.Println("Error parsing URL:", err)
	return
}

// Split the host into parts
parts := strings.Split(parsedURL.Hostname(), ".")

// Check if there is a subdomain
if len(parts) > 2 {
	subdomain := parts\[0\]
	domain := strings.Join(parts\[1:\], ".")

	baseURL := subdomain + "." + domain
	fmt.Println("Base URL with subdomain:", baseURL)
} else {
	fmt.Println("No subdomain found")
}

}

In this code snippet, we first parse the URL using url.Parse() function from the net/url package. Then, we split the hostname into parts using strings.Split() function. If the number of parts is greater than 2, we extract the subdomain and domain separately and concatenate them to get the base URL with subdomain. Otherwise, we print a message saying no subdomain was found.

You can run this code with your desired URL and it will print the base URL with subdomain if a subdomain is present in the URL.