Skip to content

Instantly share code, notes, and snippets.

@irwins
Created March 28, 2021 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irwins/619ffce1822a4ff7cc2c84d3fed11c40 to your computer and use it in GitHub Desktop.
Save irwins/619ffce1822a4ff7cc2c84d3fed11c40 to your computer and use it in GitHub Desktop.
Get random user in go
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
formatPtr := flag.String("format", "json", "format result expected. options are: Json,prettyJson,Yaml & Xml")
collSizePtr := flag.String("collectionSize", "1", "Maximum size of the result collection returned")
flag.Parse()
resp, err := http.Get("https://randomuser.me/api/?format="+*formatPtr+"&results="+*collSizePtr)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
}
@irwins
Copy link
Author

irwins commented Mar 28, 2021

./randomuser -format=yaml -collectionSize=2

results:
    -
        gender: female
        name:
            title: Ms
            first: Noémie
            last: Lévesque
        location:
            street: {number: 1106, name: 'St. Lawrence Ave'}
            city: Hudson
            state: 'Northwest Territories'
            country: Canada
            postcode: 'I1K 6X6'
            coordinates: {latitude: '-60.6796', longitude: '160.1499'}
            timezone: {offset: '-6:00', description: 'Central Time (US & Canada), Mexico City'}
        email: noemie.levesque@example.com
        login:
            uuid: d9c31d66-f839-47a9-8589-7d096bf9dbe0
            username: organickoala858
            password: laetitia
            salt: GoJDPLWw
            md5: a0c3ed27dbc68fe07bc7dc57d266be43
            sha1: ba53c689c0cd262898bfcf61b83d15bdac8d2211
            sha256: b061f7d6b68957bb11f6f4922c3f3f50e10889c4a88957405b59fa8b953d9a56
        dob:
            date: '1947-08-30T11:50:14.602Z'
            age: 74
        registered:
            date: '2012-03-05T16:32:26.231Z'
            age: 9
        phone: 795-879-0420
        cell: 104-890-8635
        id:
            name: ""
            value: null
        picture:
            large: 'https://randomuser.me/api/portraits/women/38.jpg'
            medium: 'https://randomuser.me/api/portraits/med/women/38.jpg'
            thumbnail: 'https://randomuser.me/api/portraits/thumb/women/38.jpg'
        nat: CA
    -
        gender: male
        name:
            title: Mr
            first: Vincent
            last: Kumar
        location:
            street: {number: 2051, name: 'Omahu Road'}
            city: Porirua
            state: Northland
            country: 'New Zealand'
            postcode: 78030
            coordinates: {latitude: '-8.9008', longitude: '-99.6669'}
            timezone: {offset: '0:00', description: 'Western Europe Time, London, Lisbon, Casablanca'}
        email: vincent.kumar@example.com
        login:
            uuid: 1207df0c-9684-436a-aad2-f6639a8c485e
            username: whiteelephant628
            password: spanish
            salt: rFv4Pu3R
            md5: de72d8652a123fd8c07c61981d1e25c3
            sha1: 610d4be31079cf250fda35ac90cc923a6fd26d58
            sha256: 16afae002b1d1fd0718e2eb1bd9880ba36735aa8a0389b80cd28905dc5df7990
        dob:
            date: '1996-02-10T18:56:31.165Z'
            age: 25
        registered:
            date: '2004-05-31T02:10:33.937Z'
            age: 17
        phone: (719)-967-5798
        cell: (528)-467-7855
        id:
            name: ""
            value: null
        picture:
            large: 'https://randomuser.me/api/portraits/men/1.jpg'
            medium: 'https://randomuser.me/api/portraits/med/men/1.jpg'
            thumbnail: 'https://randomuser.me/api/portraits/thumb/men/1.jpg'
        nat: NZ
info:
    seed: 80cc4ab43d23fae9
    results: 2
    page: 1
    version: '1.3'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment