Golang : Get URI segments by number and assign as variable example
Coming from PHP and CodeIgniter(framework for PHP) background. One thing that I missed is the URI helper's $this->uri->segment(n)
where it permits me to retrieve the segment by number and assign the value to string variable. For example :
URL : http://example.com/index.php/news/local/metro/crimeisup
The segment numbers would be this:
news
local
metro
crimeisup
news/local/metro/crime_is_up
is known as URL Path.
In this tutorial, we will learn how to get the URI segments by number and assign the segment as variable with Golang. See codes below :
package main
import (
"fmt"
"net/url"
"strings"
)
func main() {
rawURL := "http://example.com/index.php/news/local/metro/crime_is_up"
fmt.Println("URL : ", rawURL)
url, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
path := url.Path
uriSegments := strings.Split(path, "/")
fmt.Println(uriSegments) // count starts from 1
var metro = uriSegments[3] // assign to variable
fmt.Println("[segment 3] : ", metro)
fmt.Println("[segment 4] : ", uriSegments[4])
}
Output :
URL : http://example.com/index.php/news/local/metro/crimeisup
[ index.php news local metro crimeisup]
[segment 3] : local
[segment 4] : metro
Hang on..this is processing a static URL string. How about getting the URL from browser ?
No worries, here it is :
package main
import (
"net/http"
"strings"
)
func SayHello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}
func GetURISegment(w http.ResponseWriter, r *http.Request) {
uriSegments := strings.Split(r.URL.Path, "/")
var metro = uriSegments[3]
w.Write([]byte("[segment 3] : " + metro + "\r\n"))
w.Write([]byte("[segment 4] : " + uriSegments[4]))
}
func main() {
// http.Handler
mux := http.NewServeMux()
mux.HandleFunc("/", SayHello)
mux.HandleFunc("/news/local/metro/crime_is_up", GetURISegment)
http.ListenAndServe(":8080", mux)
}
Run this modified code and point your browser URL to :
http://localhost:8080/news/local/metro/crime_is_up
and you will see the following output :
[segment 3] : metro
[segment 4] : crimeisup
That's it. Simple URI segmenting by the numbers. Hope you will find this tutorial useful.
References :
https://ellislab.com/codeigniter/user-guide/libraries/uri.html
https://www.socketloop.com/tutorials/golang-parsing-or-breaking-down-url
See also : Golang : Parsing or breaking down URL
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+21.5k Golang : How to reverse slice or array elements order
+45.6k Golang : Read tab delimited file with encoding/csv package
+7.4k Gogland : Where to put source code files in package directory for rookie
+17.7k Golang : How to log each HTTP request to your web server?
+13.7k Golang : Get current time
+24.2k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+7.3k Golang : Rename part of filename
+15.1k Golang : invalid character ',' looking for beginning of value
+7k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+5.8k nginx : force all pages to be SSL
+10.6k PHP : Convert(cast) bigInt to string
+52k Golang : How to get struct field and value by name