why there isn't net/url#URL.Port ?

1,337 views
Skip to first unread message

Jxck

unread,
Oct 2, 2013, 10:27:18 AM10/2/13
to golan...@googlegroups.com
Hi

why URL struct dosen't has Port ?
type URL struct {
        Scheme   string
        Opaque   string    // encoded opaque data
        User     *Userinfo // username and password information
        Host     string    // host or host:port
        Path     string
        RawQuery string // encoded query values, without '?'
        Fragment string // fragment for references, without '#'
}

if url has port numer like 

URL.Host // "google.com"
URL.Port // 80
is fine.

but

if url dosen't has  port number like
URL.Parse('http://google.com/")
URL.Host // google.com
URL.Port // nil
also fine.

how about this ?

Jxck

Alexei Sholik

unread,
Oct 2, 2013, 11:31:14 AM10/2/13
to Jxck, golang-nuts
Look at the comment near Host:

Host     string    // host or host:port

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Best regards
Alexei Sholik

block.rxc...@gmail.com

unread,
Oct 2, 2013, 11:44:46 AM10/2/13
to Alexei Sholik, golang-nuts
Look at the comment near Host:

of course I know.
I mean that getting Port from Host needs helper like this
because Host dosen't always has Port as comment said.

```
func URLParse(url string) (host, path, port string) {
u, _ := urllib.Parse(url)
tmp := strings.Split(u.Host, ":")
if len(tmp) > 1 {
host, port = tmp[0], tmp[1]
} else {
host, port = tmp[0], nil
}
return
}
```

if URL has Path, I don't need any helper.

thanks
Jxck


2013/10/3 Alexei Sholik <alcos...@gmail.com>

Damian Gryski

unread,
Oct 2, 2013, 1:11:30 PM10/2/13
to golan...@googlegroups.com

DisposaBoy

unread,
Oct 2, 2013, 1:16:22 PM10/2/13
to golan...@googlegroups.com
there's already a function that does proper parsing here http://golang.org/pkg/net/#SplitHostPort

block.rxc...@gmail.com

unread,
Oct 2, 2013, 7:41:51 PM10/2/13
to DisposaBoy, golan...@googlegroups.com
>http://golang.org/pkg/net/#SplitHostPort

thanks !

but I still wonder why I need to call another function for getting Port ?

what is the "Reason" why URL dosen't has a Port as its property ?

Jxck

2013/10/03 2:16、DisposaBoy <dispo...@dby.me> のメッセージ:

there's already a function that does proper parsing here http://golang.org/pkg/net/#SplitHostPort

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/Vk6Q9W0fpyo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.

Andrew Gerrand

unread,
Oct 2, 2013, 7:48:07 PM10/2/13
to jxc k, DisposaBoy, golan...@googlegroups.com
On 3 October 2013 09:41, <block.rxc...@gmail.com> wrote:
but I still wonder why I need to call another function for getting Port ?

what is the "Reason" why URL dosen't has a Port as its property ?

These are all valid URLs:

http://example.com:/path

If Port were an int, how would you express the first two? With a zero? What about


which is also valid?

You'd need some way to distinguish between "no port" and "zero port", so I guess the Port field could be a *int (with nil meaning "no port"), but that's more clumsy to use. You could also have a separate HasPort boolean field in the struct, but that's also kinda gross.

The status quo makes a lot more sense to me. I rarely need to decompose a "host:port". You can take a "host:port" string and pass it directly to net.Dial, for instance.

Andrew

Kamil Kisiel

unread,
Oct 2, 2013, 7:53:40 PM10/2/13
to golan...@googlegroups.com, jxc k, DisposaBoy
The only place I keep running in to problems with this kind of thing is the RemoteIP field in http.Request. I forget pretty much every time that it's actually the IP *and* port. Unfortunate naming :(
 

Andrew Gerrand

unread,
Oct 2, 2013, 7:59:11 PM10/2/13
to Kamil Kisiel, golang-nuts, jxc k, DisposaBoy

On 3 October 2013 09:53, Kamil Kisiel <kamil....@gmail.com> wrote:
The only place I keep running in to problems with this kind of thing is the RemoteIP field in http.Request. I forget pretty much every time that it's actually the IP *and* port. Unfortunate naming :(

It is. It should be RemoteAddr. :-(

Andrew

Kamil Kisiel

unread,
Oct 2, 2013, 8:06:14 PM10/2/13
to golan...@googlegroups.com
Hm actually the field *is* called RemoteAddr, I guess I am confused about my confusion... 

Jxck

unread,
Oct 2, 2013, 8:55:41 PM10/2/13
to golan...@googlegroups.com, jxc k, DisposaBoy
thanks Andrew

> The status quo makes a lot more sense to me. I rarely need to decompose a "host:port". You can take a "host:port" string and pass it directly to net.Dial, for instance.

opposite case, net.Dial() requires port.
so if you call net.Dial() using URL.Host as is. you need to make sure its includes port always.
because Host doesn't ensure has port.
Host     string    // host or host:port
> You'd need some way to distinguish between "no port" and "zero port", so I guess the Port field could be a *int (with nil meaning "no port"),
I think so.

> You could also have a separate HasPort boolean field in the struct
I don't think so.
because nil means !HasPort.

if you think nil check before using considered harmful. I think so.
but what is difference between
"checking URL.Host has port using another function" and "URL.Port is nil" ?

the Biggest problem is "host:port" IS NOT "Host"

for example, if URL has
URL.Host // host
URL.Port // port
URL.Address // host:port
solves nil check problem for you.

Jxck

Andrew Gerrand

unread,
Oct 2, 2013, 9:07:13 PM10/2/13
to Jxck, golang-nuts, DisposaBoy
I don't think we need more fields in url.URL.
We can't change it anyway, so the discussion is moot.

Andrew

Jxck

unread,
Oct 2, 2013, 9:14:53 PM10/2/13
to golan...@googlegroups.com, Jxck, DisposaBoy
Andrew

I see.
thanks.

Jxck
Reply all
Reply to author
Forward
0 new messages