url.Parse() Host & Port

2,024 views
Skip to first unread message

Ryan Macy

unread,
Jan 18, 2013, 3:16:39 PM1/18/13
to golan...@googlegroups.com
Why doesn't url.Parse() break host and port apart? Is that intended?

http://play.golang.org/p/p0h4t9Q7Nq

Dave Cheney

unread,
Jan 18, 2013, 3:50:13 PM1/18/13
to Ryan Macy, golan...@googlegroups.com
If it did break apart the Host and the Port, where would it put the Port ?

http://golang.org/pkg/net/url/#URL



On Sat, Jan 19, 2013 at 7:16 AM, Ryan Macy <ry...@hackery.io> wrote:
> Why doesn't url.Parse() break host and port apart? Is that intended?
>
> http://play.golang.org/p/p0h4t9Q7Nq
>
> --
>
>

Ryan Macy

unread,
Jan 18, 2013, 4:19:24 PM1/18/13
to Dave Cheney, golan...@googlegroups.com
Well, we would have to add a field called "Port" and put it in there.

Is that something we would want to do?

Dan Kortschak

unread,
Jan 18, 2013, 6:00:18 PM1/18/13
to Ryan Macy, Dave Cheney, golan...@googlegroups.com
Even apart from the Go1 guarantee, what does that get you? If the Port field is an int you need to convert from a string to fill the field and then back to a string plus concatenation to use the Url. If it's a string you don't get much extra benefit over strings.Split(u.Host, ":").
--
 
 

Andy Balholm

unread,
Jan 18, 2013, 6:30:42 PM1/18/13
to golan...@googlegroups.com, Ryan Macy, Dave Cheney
On Friday, January 18, 2013 3:00:18 PM UTC-8, kortschak wrote:
Even apart from the Go1 guarantee, what does that get you? If the Port field is an int you need to convert from a string to fill the field and then back to a string plus concatenation to use the Url. If it's a string you don't get much extra benefit over strings.Split(u.Host, ":").

But you should use net.SplitHostPort rather than strings.Split when you do need to split them, since it handles IPv6 addresses correctly. 

Mikio Hara

unread,
Jan 18, 2013, 7:06:24 PM1/18/13
to Andy Balholm, golang-nuts, Ryan Macy, Dave Cheney
On Sat, Jan 19, 2013 at 8:30 AM, Andy Balholm <andyb...@gmail.com> wrote:

> But you should use net.SplitHostPort rather than strings.Split when you do
> need to split them, since it handles IPv6 addresses correctly.

I'm afraid that a literal IPv6 address and a literal IPv6 address in
URI are different.
E.g.,
- [fe80::1%en0] // http://tools.ietf.org/html/rfc4007
- https://[fe80::1%25en0] //
http://tools.ietf.org/html/draft-ietf-6man-uri-zoneid-06

Dan Kortschak

unread,
Jan 18, 2013, 8:42:28 PM1/18/13
to Andy Balholm, golan...@googlegroups.com, Ryan Macy, Dave Cheney
Yes. Still thinking in IPv4.

Dave Cheney

unread,
Jan 18, 2013, 8:55:28 PM1/18/13
to Dan Kortschak, Andy Balholm, golan...@googlegroups.com, Ryan Macy
Also remember that not all URLs are hrefs, eg what would the Port member mean when parsing a mailto: ?

Dan Kortschak

unread,
Jan 18, 2013, 9:10:39 PM1/18/13
to Dave Cheney, Andy Balholm, golan...@googlegroups.com, Ryan Macy
Yes. Another argument for not including a Port field.

Dave Cheney

unread,
Jan 18, 2013, 9:17:50 PM1/18/13
to Dan Kortschak, Andy Balholm, golan...@googlegroups.com, Ryan Macy
Also, what should the port be set to for

http://example.com/

?

Ryan Macy

unread,
Jan 18, 2013, 9:56:18 PM1/18/13
to Dave Cheney, Dan Kortschak, Andy Balholm, golan...@googlegroups.com
I see the reasoning for the exclusion of "Port", net.SplitHostPort solves this need.



Also, what should the port be set to for

http://example.com/

...snip...


Also remember that not all URLs are hrefs, eg what would the Port member mean when parsing a mailto: ?


If the port isn't there to parse, can it not just be 0?

Thanks for the responses!

_Ryan

DisposaBoy

unread,
Jan 22, 2013, 2:37:00 AM1/22/13
to golan...@googlegroups.com
I'm not in a position to search for a link that explains . but Yeh simple answer is no. port zero has significant meaning depending on the context. e.g. when listening on a socket it yells some? OS to pick an available port
Reply all
Reply to author
Forward
0 new messages