{"id":1764,"date":"2020-03-19T20:31:45","date_gmt":"2020-03-19T20:31:45","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=1764"},"modified":"2020-03-19T20:31:49","modified_gmt":"2020-03-19T20:31:49","slug":"find-delete-slice-golang","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/find-delete-slice-golang\/","title":{"rendered":"Find and delete in a slice in Go (Golang)"},"content":{"rendered":"\n
There can be two cases:<\/p>\n\n\n\n
Modify Original Slice<\/strong><\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n Do not modify the original slice<\/strong><\/p>\n\n\n\n Output<\/strong>:<\/p>\n\n\n\n There can be two cases: Modify Original Slice Keep copying to the original slice skipping the item which needs to be deleted Reslice at the end Output: Do not modify the original…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"class_list":["post-1764","post","type-post","status-publish","format-standard","hentry","category-tech"],"yoast_head":"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n s := []int{\"a\", \"b\", \"c\", \"a\"}\n after := findAndDelete(s, \"a\")\n fmt.Println(after)\n}\nfunc findAndDelete(s []int, item int) []int {\n index := 0\n for _, i := range s {\n if i != item {\n s[index] = i\n index++\n }\n }\n return s[:index]\n}<\/code><\/pre>\n\n\n\n
[2,3]<\/code><\/pre>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n before := []int{1, 2, 3, 1}\n after := findAndDelete(before, 1)\n fmt.Println(after)\n}\n\nfunc findAndDelete(s []int, itemToDelete int) []int {\n var new = make([]int, len(s))\n index := 0\n for _, i := range s {\n if i != itemToDelete {\n new = append(new, i)\n index++\n }\n }\n return new[:index]\n}<\/code><\/pre>\n\n\n\n
[2,3]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"