Created
September 6, 2017 07:51
-
-
Save msvitok77/87f02e196349bfe9d8eae95612f0f238 to your computer and use it in GitHub Desktop.
Get random number from random.org
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
const ( | |
min = 1 | |
max = 100 | |
) | |
func main() { | |
url := fmt.Sprintf("https://www.random.org/integers/?num=1&min=%d&max=%d&col=1&base=10&format=plain&rnd=new", min, max) | |
if resp, err := http.Get(url); err != nil { | |
log.Fatalf("http.NewRequest: %v", err) | |
} else { | |
defer resp.Body.Close() | |
if resp.StatusCode != 200 { | |
log.Fatalf("Response status code: %d", resp.StatusCode) | |
} | |
if bodyBytes, err := ioutil.ReadAll(resp.Body); err != nil { | |
log.Fatalf("ioutil.ReadAll: %v", err) | |
} else { | |
fmt.Printf("Random number [%d-%d]: %s", min, max, string(bodyBytes)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment