How can I remove string item from array of string?

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”
]

How can I do this in golang?

Is it the third value you want remove always 444444444? Or can there be a different number?

In any case, do this:

  • iterate over latlngWithTime
  • use a regular expression to remove the third value from the string
  • append the result to a new list

Yes I have to remove third array item from whole list, I have created a function for this, check this::

func GetNewRouteCoordinatesArray(coordinates []string) (routeCoordinates []string) {
	for _, coordinate := range coordinates {
		latLngArr := strings.Split(coordinate, ",")
		latString := latLngArr[0]
		lngString := latLngArr[1]
		/* append latlangstring values in routeCoordinates array of string */
		routeCoordinates = append(routeCoordinates, latString+`,`+lngString)
	}
	return routeCoordinates
}

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.

3 Likes

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.

1 Like

https://goplay.space/#zmN_ccxWmv5

Make a new string-slice with the same size as the old one. Fill it with substring-slices from 0 up to but not including the last comma

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)
}