Skip to content

Instantly share code, notes, and snippets.

@gerep
Created May 9, 2018
Embed
What would you like to do?
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