Get and print IP address from https://whatismyipv6.buddy.wtf/json
							parent
							
								
									a747567d84
								
							
						
					
					
						commit
						a63f9d657e
					
				@ -0,0 +1,34 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func main() {
 | 
				
			||||||
 | 
						result, err := getIP("https://whatismyipv6.buddy.wtf/json")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Println(result.IP)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type whatIsMyIPResult struct {
 | 
				
			||||||
 | 
						IP      string `json:ip`
 | 
				
			||||||
 | 
						Version string `json:version`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func getIP(url string) (*whatIsMyIPResult, error) {
 | 
				
			||||||
 | 
						result, err := http.Get(url)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer result.Body.Close()
 | 
				
			||||||
 | 
						ipResult := &whatIsMyIPResult{}
 | 
				
			||||||
 | 
						if err := json.NewDecoder(result.Body).Decode(ipResult); err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return ipResult, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
					Loading…
					
					
				
		Reference in New Issue