creating URL using protocol,port,host

1,936 views
Skip to first unread message

Abhinav Srivastava

unread,
Nov 12, 2013, 11:47:26 PM11/12/13
to golan...@googlegroups.com
Hi,

I wanted to create the URL string like in java e.g.

String host = InetAddress.getLocalHost().getHostAddress();
for (int i = 0; i < 10; i++) { URL url = new URL("http", host, port + i, ""); stud.createMyPage(url.toString()) } I want to achieve the same in Go. I was thinking of using something like : name,err:=os.Hostname() addrs,err:=net.LookupHost(name) Please guide me further. Thanks Abhinav


StalkR

unread,
Nov 13, 2013, 12:08:41 PM11/13/13
to Abhinav Srivastava, golang-nuts
Hi Abhinav, net/url module can help: http://godoc.org/net/url#URL
Build your URL struct then call .String().


--
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.

Abhinav Srivastava

unread,
Nov 13, 2013, 8:23:05 PM11/13/13
to golan...@googlegroups.com, Abhinav Srivastava, sta...@stalkr.net
Hi StalkR,

Thanks for the reply..I looked into net/url package for the URL string creation but i donot know if I am doing the right way..

package main

import (
    "fmt"
    "net"
    "os"
)

func main() {
    name,_:=os.Hostname()
    addrs,_:=net.LookupHost(name)

    fmt.Println(addrs)
}

ouput :

[fe80::79b0:e252:515f:eb4d fe80::4d0:e6b0:7ece:854d 172.31.81.78 2001:0:9d38:6abd:4d0:e6b0:7ece:854d]
 
This is not how I want, like in JAVA , the code in my previous post will return the hostaddress in readable string..how to achieve the same in Go.
Also for url string..

Can I do something like this :


for i:=0;i<10;i++{
   url=URL{"https",nil,nil,addrs,..........}  //similarly setting other properties and using addrs from above.
  stud.createMyPage(url.String())
}

Is this the right way to do?

Please guide..

Thanks Abhinav

Abhinav Srivastava

unread,
Nov 13, 2013, 9:04:21 PM11/13/13
to golan...@googlegroups.com, Abhinav Srivastava, sta...@stalkr.net
Sorry my bad..i should reframe some part of my query..how can i get only the IPV4 address since :
addrs,_:=net.LookupHost(name)
 is returning all the associated address to the "name".

Thanks 

Abhinav Srivastava

unread,
Nov 13, 2013, 9:29:03 PM11/13/13
to golan...@googlegroups.com, Abhinav Srivastava, sta...@stalkr.net
got the IPv4 extracted :

package main

import (
    "fmt"
    "net"
    "os"
)

func main() {

    //var newip IP
    name,_:=os.Hostname()
    fmt.Println (name)
    addrs,_:=net.LookupHost(name)
    for _,val:=range(addrs) {
        newip:=net.ParseIP(val)
        if newip.To4()==nil {
            continue
        }else{
        fmt.Println(val)
    }   }
}

Please guide me if anything easier than this and also for URL , am i using the right way..

Thanks 

Abhinav

StalkR

unread,
Nov 14, 2013, 8:27:44 AM11/14/13
to Abhinav Srivastava, golang-nuts
Your code for IPv4 is good.

You can use net/url like this: u := url.URL{Scheme: "http", Host: val} then fmt.Println(u.String())
For a complete example see: http://play.golang.org/p/2n_0XfR8Xb

Also, don't discard errors:
name,err := os.Hostname()
if err != nil {
  fmt.Println("error:", err)
  os.Exit(1)
}
Reply all
Reply to author
Forward
0 new messages