Skip to content

Instantly share code, notes, and snippets.

@gerep
Created May 9, 2018 19:16
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 gerep/ed9bc86642449d76024c4829039d5cda to your computer and use it in GitHub Desktop.
Save gerep/ed9bc86642449d76024c4829039d5cda to your computer and use it in GitHub Desktop.
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