開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
入門Goプログラミング (Nathan Youngman(著)、Roger Peppé(著)、吉川 邦夫(監修, 翻訳)、翔泳社)のUNIT 6(ネズミの穴を下って)、LESSON 28(エラーは人の常)の練習問題の解答を求めてみる。
コード
package main
import (
"fmt"
"net/url"
"os"
)
func main() {
rawurls := []string{
"https://a b.com/",
"https://www.mkamimura.com/",
}
for _, rawurl := range rawurls {
u, err := url.Parse(rawurl)
if err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintf(os.Stderr, "%#v\n", err)
if e, ok := err.(*url.Error); ok {
fmt.Fprintln(os.Stderr, e.Op)
fmt.Fprintln(os.Stderr, e.URL)
fmt.Fprintln(os.Stderr, e.Err)
}
continue
}
fmt.Printf("%v\n%#[1]v\n", u)
}
}
入出力結果(Zsh、PowerShell、Terminal)
% go run url.go
parse "https://a b.com/": invalid character " " in host name
&url.Error{Op:"parse", URL:"https://a b.com/", Err:" "}
https://www.mkamimura.com/
&url.URL{Scheme:"https", Opaque:"", User:(*url.Userinfo)(nil), Host:"www.mkamimura.com", Path:"/", RawPath:"", ForceQuery:false, RawQuery:"", Fragment:""}
% go run url.go
parse "https://a b.com/": invalid character " " in host name
&url.Error{Op:"parse", URL:"https://a b.com/", Err:" "}
parse
https://a b.com/
invalid character " " in host name
https://www.mkamimura.com/
&url.URL{Scheme:"https", Opaque:"", User:(*url.Userinfo)(nil), Host:"www.mkamimura.com", Path:"/", RawPath:"", ForceQuery:false, RawQuery:"", Fragment:""}
%
0 コメント:
コメントを投稿