This question already has an answer here:
Sorting by slice fields [duplicate]
(1 answer)
Closed 3 years ago.
I have a slice in golang which looks something like this.
list := []TripInfo{
{
TripID: "uuid2",
infov:true
},
{
TripID: "uuid1",
infov:false
},
}
How can I sort it based on TripID so that it looks something like this?
list := []TripInfo{
{
TripID: "uuid1",
infov:false
},
{
TripID: "uuid2",
infov:true
},
}
TripInfo is a struct
type TripInfo struct {
TripUUID string
infov bool
}
The sort pkg is your friend:
import "sort"
// sort slice in place
sort.Slice(list, func(i, j int) bool {
return list[i].TripID < list[j].TripID
})
Playground version.
Related
I need help for transforming this input map into the output map. I try with switch/case and for but I didn't succeed it. Thanks a lot !
Input :
Values{
"toto_voiture_brand": Ad{
"CITROEN": "CITROEN",
},
"toto_voiture_model": Ad{
"CITROEN_toto": "C3",
},
"toto_moto_brand": Ad{
"KAWASAKI": "KAWASAKI",
},
"toto_moto_model": Ad{
"KAWASAKI_tata": "Ninja 1000 SX",
},
"toto_camion_brand": Ad{
"RENAULT": "RENAULT",
"PEUGEOT": "PEUGEOT",
},
"toto_camion_model": Ad{
"RENAULT_toto": "J5",
"PEUGEOT_tata": "255",
},
},
}
Output
Values{
"toto_voiture_model": {
"Citroen": {
{Value: "C3"},
},
},
"toto_moto_model": {
"Kawasaki": {
{Value: "Ninja 1000 SX"},
},
},
"toto_camion_model": {
"RENAULT": {
{Value: "J5"},
},
"PEUGEOT": {
{Value: "255"},
},
},
}
I've tried with switch case and loop for and map. But I don't have the result attendee, I didn't found how to match every map, key and value. Thanks a lot
I should have managed what you need with the following code:
package main
import (
"encoding/json"
"fmt"
"strings"
)
type Output struct {
Value string `json:"Value"`
}
func main() {
// declare output
output := make(map[string]map[string]Output, 0)
// input
input := make(map[string]map[string]string, 0)
input["toto_voiture_brand"] = map[string]string{
"CITROEN": "CITROEN",
}
input["toto_voiture_model"] = map[string]string{
"CITROEN_toto": "C3",
}
input["toto_moto_model"] = map[string]string{
"KAWASAKI_tata": "Ninja 1000 SX",
}
input["toto_camion_model"] = map[string]string{
"RENAULT_toto": "J5",
"PEUGEOT_tata": "255",
}
// transformation
for k, v := range input {
if strings.HasSuffix(k, "_model") {
tempMap := make(map[string]Output, len(v))
for kk, vv := range v {
key := strings.Split(kk, "_")[0]
tempMap[key] = Output{
Value: vv,
}
}
output[k] = tempMap
}
}
data, _ := json.MarshalIndent(&output, "", "\t")
fmt.Println(string(data))
}
I put some comments within the code just to separate sections. The first two parts are only supposed to define your input and output variables.
The section starting with // transformation is a good candidate to become a function but I preferred to leave it within the main function for demo purposes. Let me recap what's happens in the loop:
You range over the entries of your input variable
If the key has the suffix _model, you take it into consideration
You define a locally-scoped map (called tempMap) of type map[string]Output with the right number of elements that we're gonna add
You range over the v variable (that's why we're dealing with nested maps)
For each item, you're gonna add an entry to the tempMap
At the end of the nested loop, you add an entry to the parent map (output)
The last part is only for printing a beautiful JSON that can be easily read and checked.
Note that this code is simplified just to show off how to achieve your goal, adjust it before putting it into production.
Let me know if this helps, thanks!
This question already has answers here:
Can I construct a slice of a generic type with different type parameters?
(2 answers)
Closed 4 months ago.
Lets jump straight into a code snippet.
type Animal[T any] struct {
Name string
Type string
Params T
}
type DogParams struct {
TailLength int
}
type CatParams struct {
MeowVolume int
}
type Dog = Animal[DogParams]
type Cat = Animal[CatParams]
type Tiger = Animal[CatParams]
var animals = []Animal[any]{
{
Name: "biggie doggie",
Type: "dog",
Params: DogParams{TailLength: 5},
},
{
Name: "persia",
Type: "cat",
Params: CatParams{MeowVolume: 2},
},
}
What is the best way to convert an animal from Animal[any] to Animal[CatParams] ?
I have tried
cat, ok := any(animals[1]).(Cat)
and it's not ok.
Thanks in advance!
Following the comments from #Volker and #Pak Uula, you can stick them in an interface{} slice, but this isn't run-time dynamic typing. A Cat is still a Cat:
var animals = []interface{}{
Dog{
Name: "biggie doggie",
Type: "dog",
Params: DogParams{TailLength: 5},
},
Cat{
Name: "persia",
Type: "cat",
Params: CatParams{MeowVolume: 2},
},
}
I am trying to add an array of strings into the content part of a struct with the Id as the array index. I have the code working for one element, but get various errors when I try and add the loop. Any ideas are welcome. I am using Go.
func buildRequest(s []string) []*storepb.LongStoreRequest {
// ss:= []storepb.LongStoreRequest
// int32 i =0 stringv := s[0]
// for i := 0; i < len(s); i++ {
// println(i, apps[i])
ss := []*storepb.LongStoreRequest{
&storepb.LongStoreRequest {
Msg: &storepb.StoreMessage{
Content: stringv,
Account: "trevor3",
Parent: "parentrec",
Id: 0,
},
},
} // }
return ss
}
If I understand your description correctly, you want to build an array of LongStoreRequests, where each element corresponding to an item in the string array, with Id giving the array index. If that's really what you need, something like this should work:
ss := []*storepb.LongStoreRequest{}
for i,str:=range s {
ss=append(ss,&storepb.LongStoreRequest {
Msg: &storepb.StoreMessage{
Content: str,
Account: "trevor3",
Parent: "parentrec",
Id: i,
}})
}
I tried hard to find example as mine, but although bunch of questions are very similar, I wasnt able to understand what I am doing wrong.
I am very new to golang, and I am trying to implement game of life.
here is part of my code
// Species struct
type Species struct {
xPos int32
yPos int32
isAlive bool
willChangeState bool
rect sdl.Rect
neighbours []Species
}
type Ecosystem struct {
community []Species
}
func (ecos *Ecosystem) addSpecies(sp Species) {
ecos.community = append(ecos.community, sp)
}
func (s *Species) addNeigbour(neigbour Species) {
s.neighbours = append(s.neighbours, neigbour)
}
I want to distribute neighbours as in this function
func (ecos *Ecosystem) distributeNeighbours() {
for _, species := range ecos.community {
for _, potentionalNeighbour := range ecos.community {
if math.Abs(float64(species.xPos-potentionalNeighbour.xPos)) <= speciesSize && math.Abs(float64(species.yPos-potentionalNeighbour.yPos)) <= speciesSize {
if species.xPos == potentionalNeighbour.xPos && species.yPos == potentionalNeighbour.yPos {
continue
}
species.addNeigbour(potentionalNeighbour)
}
}
fmt.Println(len(species.neighbours)) // works here
}
for _, s := range ecos.community {
fmt.Println(len(s.neighbours)) //prints 0
}
}
So I guess I have to manage it with pointers - some issue like species in first loop is copy of that species in community, so original species does not gain any neigbours. But I dont know how to fix it.
Try slice of pointer, like this:
// Species struct
type Species struct {
xPos int32
yPos int32
isAlive bool
willChangeState bool
rect sdl.Rect
neighbours []*Species
}
type Ecosystem struct {
community []*Species
}
I have a json file (nested json) that I am unmarshalling its content into a map[string]interface. Now I have to implement pagination as the data is large. The client side will send as a query parameter the desired page, how can I slice the data I have?
This is a snippet of the data I am dealing with:
"packages":{
"pkg1": {
"meta": {
"description": "description1",
"name": "pkg1.1"
},
"name": "pkg1.1"
},
"pkg2": {
"meta": {
"description": "description2",
"name": "pkg2.2"
},
"name": "pkg2.2"
},
}
So what I did is that I recursively iterated through the data and created an array of a custom type containing the data I need (name, description) for each entry so that I can use it for pagination. Here is the code I used:
type Object struct {
name string
description string
}
func iterate(aMap map[string]interface{}, result *[]Object){
for key, val := range aMap {
switch val.(type) {
case map[string]interface{}:
if(key == "meta"){
switch reflect.TypeOf(val).Kind() {
case reflect.Map:
s := reflect.ValueOf(val)
var tmpData Object
if(s.MapIndex(reflect.ValueOf("name")).IsValid()){
tmpData.name = s.MapIndex(reflect.ValueOf("name")).Interface().(string)
}
if(s.MapIndex(reflect.ValueOf("description")).IsValid()){
tmpData.description = s.MapIndex(reflect.ValueOf("description")).Interface().(string)
}
*result = append(*result, tmpData)
}
}
iterate(val.(map[string]interface{}), result)
default: //DO NOTHING!!
}
}
}
If you're doing pagination, somewhere the data must be represented as a list instead of an object? I assume at some place in your JSON, you have a list of items, otherwise pagination doesn't make sense.
It shouldn't be very hard, something simple like this should work:
const (
itemsPerPage = 10
)
var data []map[string]interface{}
// pages start at 1, can't be 0 or less.
func GetDataPage(page int) []map[string]interface{} {
start := (page - 1) * itemsPerPage
stop := start + itemsPerPage
if start > len(data) {
return nil
}
if stop > len(data) {
stop = len(data)
}
return data[start:stop]
}
You are unmarshalling your json into a map which has no order by itself. In order to be able to paginate your results you need to order them in some way.
One way of doing it is to sort your data and then store it into an array. But in order to paginate you need to have ordered data and that is not possible with a map.