<p>So i am trying to have my Go program connect to <a href="http://checkip.amazonaws.com/">http://checkip.amazonaws.com/</a> and return the IP address shown, so the main thread can do things with it (example Println)</p>
<p>Every time i try doing this with http.get i get a return of </p>
<blockquote>
<p>0x401360</p>
</blockquote>
<p>This needs to be a separate function from main, so it can be called when needed.</p>
<p>I am new to GoLANG, my main language is VB.net so its a little hard for me.</p>
<hr/>**评论:**<br/><br/>iamedwards: <pre><p>Please provide your code, sounds like you're printing the pointer to your function and not the results. </p></pre>DeedleFake: <pre><p>I'm not sure why you're trying to do this, but this potentially inefficient implementation should work:</p>
<p>+<a href="/u/CompileBot" rel="nofollow">/u/CompileBot</a> go --include-errors</p>
<pre><code>package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
func checkIP() (string, error) {
rsp, err := http.Get("http://checkip.amazonaws.com")
if err != nil {
return "", err
}
defer rsp.Body.Close()
buf, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return "", err
}
return string(bytes.TrimSpace(buf)), nil
}
func main() {
ip, err := checkIP()
if err != nil {
panic(err)
}
fmt.Printf("CheckIP reports %q", ip)
}
</code></pre></pre>SaturnsVoid: <pre><p>Huh, didn't know that Go had ability's to add quotes to strings and stuff like that.. <a href="https://golang.org/pkg/fmt/" rel="nofollow">https://golang.org/pkg/fmt/</a></p></pre>SaturnsVoid: <pre><p>How is it inefficient? As far as i know this is the only way to get the systems external IP...</p>
<p>I need it in another function for calling at anytime, like so;</p>
<p>Server tells client to tell its IP -> Client tells IP to server -> Server tells Client to change IP, then asks for the new IP</p>
<p>There isn't a set time i want it to check the IP, just whenever i send to command to do so.</p>
<p>I am new so i may not know, is there a way do do this simpler?</p></pre>SpaceDetective: <pre><p>Is this any use?<br/>
<a href="http://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go" rel="nofollow">http://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go</a></p></pre>SaturnsVoid: <pre><p>Sadly only gives back the local IP, 192.168.XX.XX</p></pre>SpaceDetective: <pre><p>OK you mean you're going through a proxy/nat. I guess your original plan is on the right track then...</p></pre>IntellectualReserve: <pre><p>I see the solution you are trying to implement, and I understand the question you are trying to get answered so you can continue with the solution you already have in mind. What is the problem this solution is supposed to solve? </p></pre>CompileBot: <pre><p>Output:</p>
<pre><code>panic: Get http://checkip.amazonaws.com: dial tcp: lookup checkip.amazonaws.com: no such host
goroutine 1 [running]:
main.main()
/home/Ua4LWh/prog.go:28 +0x62
goroutine 17 [syscall, locked to thread]:
runtime.goexit()
/usr/local/go/src/runtime/asm_386.s:2287 +0x1
</code></pre>
<p><a href="http://ideone.com/5X6gbd" rel="nofollow"><sup>source</sup></a> <sup>|</sup>
<a href="http://www.reddit.com/r/CompileBot/wiki" rel="nofollow"><sup>info</sup></a> <sup>|</sup>
<a href="https://github.com/renfredxh/compilebot" rel="nofollow"><sup>git</sup></a> <sup>|</sup>
<a href="http://www.reddit.com/message/compose?to=compilebot&subject=Report%20Abuse&message=--report%20https%3A//www.reddit.com/r/golang/comments/3l71g4/help_function_to_return_the_users_external_ip/cv3pj7r%20Include%20your%20reason%20for%20reporting%20here." rel="nofollow"><sup>report</sup></a></p></pre>DeedleFake: <pre><p>It doesn't work in whatever sandbox CompileBot runs in, though.</p></pre>SaturnsVoid: <pre><p>It wouldn't, Its setup to block out-going connections for security.</p></pre>sc28: <pre><p>How about this?
curl <a href="http://169.254.169.254/latest/meta-data/public-ipv4" rel="nofollow">http://169.254.169.254/latest/meta-data/public-ipv4</a></p>
<p>This assumes that your Go program is running inside an EC2 instance - just use that metadata URL</p></pre>SaturnsVoid: <pre><p>Running it as a standalone application an windows exe.</p></pre>Mteigers: <pre><p>Something like <a href="http://github.com/polds/MyIP" rel="nofollow">github.com/polds/MyIP</a> ?</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传