"golang diff of string arrays" Code Answer's
You're definitely familiar with the best coding language Go that developers use to develop their projects and they get all their queries like "golang diff of string arrays" answered properly. Developers are finding an appropriate answer about golang diff of string arrays related to the Go coding language. By visiting this online portal developers get answers concerning Go codes question like golang diff of string arrays. Enter your desired code related query in the search bar and get every piece of information about Go code related question on golang diff of string arrays.
golang diff of string arrays
package main
import "fmt"
func difference(slice1 []string, slice2 []string) []string {
var diff []string
// Loop two times, first to find slice1 strings not in slice2,
// second loop to find slice2 strings not in slice1
for i := 0; i < 2; i++ {
for _, s1 := range slice1 {
found := false
for _, s2 := range slice2 {
if s1 == s2 {
found = true
break
}
}
// String not found. We add it to return slice
if !found {
diff = append(diff, s1)
}
}
// Swap the slices, only if it was the first loop
if i == 0 {
slice1, slice2 = slice2, slice1
}
}
return diff
}
func main() {
slice1 := []string{"foo", "bar", "hello"}
slice2 := []string{"foo", "world", "bar", "foo"}
fmt.Printf("%+v\n", difference(slice1, slice2))
}
Source: stackoverflow.com
All those coders who are working on the Go based application and are stuck on golang diff of string arrays can get a collection of related answers to their query. Programmers need to enter their query on golang diff of string arrays related to Go code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about golang diff of string arrays for the programmers working on Go code while coding their module. Coders are also allowed to rectify already present answers of golang diff of string arrays while working on the Go language code. Developers can add up suggestions if they deem fit any other answer relating to "golang diff of string arrays". Visit this developer's friendly online web community, CodeProZone, and get your queries like golang diff of string arrays resolved professionally and stay updated to the latest Go updates.