Home > Blockchain >  How to get the page name in go?
How to get the page name in go?

Time:09-05

I have a function which should get the page name and print it, for example, if the URL is http://localhost:8080/login.html the function should print login.html

CodePudding user response:

If you only need to parse the URL you can use below:

package main

import (
    "fmt"
    "net/url"
)

func main() {
    URL := "http://localhost:8080/login.html"

    name, err := getPageName(URL)

    if err != nil {
        panic(err)
    }

    fmt.Println(name)
}

func getPageName(URL string) (string, error) {
    u, err := url.Parse(URL)

    if err != nil {
        return "", err
    }

    return u.Path[1:], nil // To remove initial /

}

If you need to get page's HTML and parse the title from <head> you can use go-query

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/PuerkitoBio/goquery"
)

func main() {
    URL := "https://stackoverflow.com"

    res, err := http.Get(URL)

    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()

    if res.StatusCode != 200 {
        log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
    }

    // Load the HTML document
    doc, err := goquery.NewDocumentFromReader(res.Body)
    if err != nil {
        log.Fatal(err)
    }

    title := doc.Find("title").Text()

    fmt.Println(title)
}

CodePudding user response:

You can do this to get the page name:

func GetPageName(address string) (string, error) {
    u, err := url.Parse(address)
    if err != nil {
        return "", err
    }

    params := strings.Split(u.Path, "/")

    // Index page
    if len(params) == 0 || (len(params) == 1 && params[0] == "") {
        return "", nil
    } else {
        pageName := params[len(params)-1]
        // Has trailing slash
        if pageName == "" {
            return params[len(params)-2], nil
        }
        // Doesn't have trailing slash
        return pageName, nil
    }
}

If the url address is the index page address, for example host.com or host.com/ it returns an empty string.
Otherwise returns the page name for the given url. For example test for host.com/test and host.com/test/ and host.com/path/test.

  • Related