How to delete an empty value in a slice in golang?

Published: Monday, Apr 6, 2015 Last modified: Monday, May 27, 2024

There is no delete in a slice, since in golang slices are not that high level. You can think of them as variable-length c arrays. Do take time to read

Given a slice like:

s := []string{"foo", "", "bar", ""}

How do you remove the empty values simply? i.e. printing an end result:

["foo" "bar"]

func delete_empty (s []string) []string {
	var r []string
	for _, str := range s {
		if str != "" {
			r = append(r, str)
	return r


Another approach is to define a helper function, ala