Created
May 9, 2018 19:16
-
-
Save gerep/ed9bc86642449d76024c4829039d5cda to your computer and use it in GitHub Desktop.
This file contains 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" | |
"math/rand" | |
"reflect" | |
"time" | |
) | |
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
type T struct { | |
S string | |
I int | |
} | |
func main() { | |
rand.Seed(time.Now().UnixNano()) | |
y := []T{ | |
T{"string1", 1}, | |
T{"string2", 2}, | |
T{"string3", 3}, | |
T{"string4", 4}, | |
} | |
result := []string{} | |
for _, x := range y { | |
v := reflect.ValueOf(x) | |
r := "" | |
comma := "" | |
for i := 0; i < v.NumField(); i++ { | |
r = r + fmt.Sprintf("%s%v", comma, v.Field(i)) | |
comma = "," | |
} | |
result = append(result, r) | |
} | |
fmt.Println(result) | |
fmt.Println(RandStringBytes(10)) | |
fmt.Println(RandIntBytes(10)) | |
fmt.Println(RandFloats(1.10, 101.98)) | |
} | |
func RandStringBytes(n int) string { | |
b := make([]byte, n) | |
for i := range b { | |
b[i] = letterBytes[rand.Intn(len(letterBytes))] | |
} | |
return string(b) | |
} | |
func RandIntBytes(n int) int { | |
return rand.Intn(100) | |
} | |
func RandFloats(min, max float64) float64 { | |
return rand.Float64() * (max - min) | |
} | |
func RandFloatsList(min, max float64, n int) []float64 { | |
res := make([]float64, n) | |
for i := range res { | |
res[i] = min + rand.Float64()*(max-min) | |
} | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment