Help us understand the problem. What is going on with this article?

Golang で URL から charset を取得するのを書いたのですが、他にもっとよい方法があるとおもうのです...

More than 1 year has passed since last update.

他によい方法がある気がするのですが...わからないので、書いてみました。

URL から、その charset を取得する。

main.go
package main

import (
    "fmt"
    "net/http"
    "regexp"
    "github.com/PuerkitoBio/goquery"
)


func getCharset(url string) (charset string) {
    res, err := http.Get(url)
    if err != nil {
        // handle error
    }
    defer res.Body.Close()

    doc, _ := goquery.NewDocument(url)
    doc.Find("meta").Each(func(_ int, s *goquery.Selection) {

        // <meta charset="utf-8"> の場合
        charsetAttr, _ := s.Attr("charset")
        if charsetAttr != "" {
            charset = charsetAttr
        }

        // <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> の場合
        httpEquiv, _ := s.Attr("http-equiv")
        if httpEquiv == "Content-Type" {
            content, _ := s.Attr("content")
            rep := regexp.MustCompile(`.*charset=(.+)`)
            charsetStr := rep.ReplaceAllString(content, "$1")
            if charsetStr != "" {
                charset = charsetStr
            }
        }
    })
    return charset
}

func main() {
    url := "https://qiita.com/trend"
    a := getCharset(url)
    fmt.Printf("%v", a)
}
$ go run main.go
UTF-8%
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away