I need to remove this third string item from array of latlngWithTime, I just need first two values and want to remove every third value.
Structure I have::
latlngWithTime = [
“32.38537857, 67.6247667, 444444444”,
“32.38537857, 67.6247667, 444444444”,
“32.38537857, 67.6247667, 444444444”,
“32.38537857,67.6247667,444444444”
]
Structure I want from above as follows::
latlng = [
“32.38537857, 67.6247667”,
“32.38537857, 67.6247667”,
“32.38537857, 67.6247667”,
“32.38537857,67.6247667”
]
Is there any optimized way to get exact same result I mentioned above? This resolved my issue, but I am looking for well optimized code, because I have thousand of entries for single person. There is a huge data I have to iterate here.
Wouldn’t a much better approach be to properly “parse” the data into structs?
type struct With {
lang string
long string
randomNumber string
}
type WithOut {
lang string
long string
}
func parseWith(input string) With {
values := strings.Split(input, ',')
return With{
lang: values[0],
long: values[1],
randomNumber: values[2],
}
}
func (w With) ToWithOut() WithOut {
return WithOut {
long: w.long,
lang: w.lang,
}
}
This is a quickshot though without any verification, but the idea should be clear.
As a bonud you can implement fmt.Stringer to get your old string representation back when necessary.
Make sure to call strings.TrimSpace on each coordinate. Your input looks like it contains whitespace which, when properly parsed, should not be part of the data.
If there’s huge amount of data why not use goroutines ? You can split your original slices and send splits to different goroutines doing the same as your algo. This is the go way. Try next code with different split size. I leave to you to join the final result.
package main
import (
"fmt"
"sync"
"strings"
)
func GetNewRouteCoordinatesArray(coordinates []string) (routeCoordinates []string) {
for _, coordinate := range coordinates {
latLngArr := strings.Split(coordinate, ", ")
latString := latLngArr[0]
lngString := latLngArr[1]
routeCoordinates = append(routeCoordinates, latString+`,`+lngString)
}
return
}
func main() {
latlngWithTime := []string {"32.38537857, 67.6247667, 444444444","32.38537857, 67.6247667, 444444444","32.38537857, 67.6247667, 444444444","32.38537857, 67.6247667, 444444444"}
split:=1
n := int(len(latlngWithTime)/split)
m := len(latlngWithTime )%split
if m !=0 {
n+=1
}
var wg sync.WaitGroup
results := make([][]string,n)
for i:=0;i<n;i++ {
wg.Add(1)
go func (k int) {
defer wg.Done()
end := (k+1)*split
if end > len(latlngWithTime) {
end = len(latlngWithTime)
}
results[k] = GetNewRouteCoordinatesArray(latlngWithTime[k*split:end])
}(i)
}
wg.Wait()
fmt.Println(results)
}