Hi I'm basically struggling to avoid redundancy in my Code.
Here is my Struct:
type Person struct {
Name string
Text string
Status string
}
Here is my Func:
func ReworkElementsFromClient(w http.ResponseWriter, r *http.Request){
var persons []Person
err := json.NewDecoder(r.Body).Decode(&persons)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(persons) == 0 {
err = json.NewEncoder(w).Encode(persons)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
persons = append([]Person{persons[0]}, persons...)
persons = persons[:len(persons)-1]
//Trying to avoid redundancy here
persons[0].Name = CreateRandomChars(CreateRandomNumbers(8, 16))
persons[0].Text = CreateRandomChars(CreateRandomNumbers(8, 16))
persons[0].Status = CreateRandomChars(CreateRandomNumbers(8, 16))
err = json.NewEncoder(w).Encode(persons)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
How to avoid calling each properties at Person[0]
to randomizing them?