Search in struct array - database

I have a struct defined like this :
type Issues struct {
RedmineIssue string
GitlabIssue string
}
Then I get the list from the DB
database.Find(&Issues)
Then I have another array
redmineIssues []redmine.Issue
Is there any way to search issues in my array Issues that are also in the array redmineIssues base on field RedmineIssue ( string ) ?
Today here is what I'm doing
database.Find(&Issues)
redmineIssue := []string{}
for _, issueRedmine := range Issues {
redmineIssue = append(redmineIssue, issueRedmine.RedmineIssue)
}
gitlabissues := []string{}
for _, issueGitlab := range Issues {
gitlabissues = append(gitlabissues, issueGitlab.GitlabIssue)
}
Then I can compare with another array I have
for _, issueR := range IssueFromRedmineWS {
inArray, _ := in_array(issueR.Id, redmineIssue)
if !inArray {
// The issue is not in the DB
}
}
Any idea on how to optimize this and to make it cleaner?

A for loop is the right way to go. Assuming the code you pasted actually works, you can merge those into a single for loop, the way you have it written.
database.Find(&Issues)
redmineIssue := []string{}
gitlabissues := []string{}
for _, issue := range Issues {
redmineIssue = append(redmineIssue, issue.RedmineIssue)
gitlabissues = append(gitlabissues, issue.GitlabIssue)
}
But this assumes that Issues is an array. Which doesn't match the rest of your question. According to your stated definition of Issues, the code you posted won't even compile, though. So I don't know if this code will work for you, either.
You can provide another micro-optimization by pre-allocating the arrays:
database.Find(&Issues)
redmineIssue := make([]string{}, 0, len(Issues))
gitlabissues := make([]string{}, 0, len(Issues))

Related

Golang: map with multiple values per key

func ParseportsFromFile(file string) (map[string]string, error) {
buf, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
ret := [make(map[string]string)]
rows := strings.Split(string(buf), "\n")
for _, row := range rows {
kvs := strings.SplitN(row, "=", 2)
if len(kvs) == 2 {
ret[strings.TrimSpace(kvs[0])] = strings.TrimSpace(kvs[1])
}
}
return ret, nil
}
This function allows me to read a file like that :
user1=123
user1=321
user2=124
However, the data return is
map[user1:321 user2:124]
So that mean user1=123 has been overwritten with user1=321
How to avoid that ?
How to create an array like
map[user1:[123,321], user2: 124]
to avoid an item to overwrite another ?
Since go is strongly typed, it would be easier to make it right away a map of slices. See the http.Header type, for example. They had the same problem to solve when designing it.
In your case, this could look something like this:
result := make(map[string][]string)
for _, row := range rows {
parts := strings.Split(row, "=")
key := parts[0]
value := parts[1]
result[key] = append(result[key], value)
}
https://go.dev/play/p/5uRH-aQmATR
Otherwise, you need to use interface{} (any) so you can have both, string and []string, but the logic to get that done would be more complex, and using it would be also more complex since you always need to check what it is and do type assertion and so on. After all, I would not recommend it.

Deepcopying map containing pointer to structure

I currently have a map which is as follows
licFeatureMem = make(map[string][]*common.Feature)
I need to make a copy of the map to pass into some function which modified the same so that i don't lose the original copy
modFeatureMem := make(map[string][]*common.Feature)
for key, lst := range licFeatureMem {
var newFtLst []*common.Feature
for _, info := range lst {
newFtLst = append(newFtLst, info)
}
modFeatureMem[key] = &newFtLst
}
What is see is when modFeatureMem is modified, original licFeatureMem also gets modified. Can someone please help me on how to do this?
The copied map has the same pointers to the common.Feature instances the original map has. Any modification to common.Feature will be visible on both maps. To get a truly deep copy, you have to copy those objects as well:
for key, lst := range licFeatureMem {
var newFtLst []*common.Feature
for _, info := range lst {
newInfo:=*info
newFtLst = append(newFtLst, &newInfo)
}
modFeatureMem[key] = &newFtLst
}

How to finding result of intercept of two slices in golang

There is two slices in string type.I want to finding intercept result in set from the two slices in golang.I want to find the best solution rather than iterating each slice.
first_slice := []string{"F8-E7-1E-14-AE-00", "F8-E7-1E-14-D0-30", "84-18-3A-2F-05-E0","84-18-3A-2F-05-E8" }
second_slice := []string{"F8-E7-1E-14-D0-30", "84-18-3A-2F-05-E8","F8-E7-1E-54-AE-08"}
Output:
result_slice := []string{"F8-E7-1E-14-D0-30", "84-18-3A-2F-05-E8"}
I have use following approaches but it is not best approaches for large data set.
var result_slice *[]string
for _, i := range first_slice {
for _, x := range second_slice {
if i == x {
&result_slice.append(i)
}
}
}
Appreciate if give me good solution that.
firstSlice := []string{"F8-E7-1E-14-AE-00", "F8-E7-1E-14-D0-30",
"84-18-3A-2F-05-E0", "84-18-3A-2F-05-E8"}
secondSlice := []string{"F8-E7-1E-14-D0-30", "84-18-3A-2F-05-E8",
"F8-E7-1E-54-AE-08"}
resultSlice := []string{}
checkMap := map[string]struct{}{}
for _, addr := range firstSlice {
checkMap[addr] = struct{}{}
}
for _, addr := range secondSlice {
if _, ok := checkMap[addr]; ok {
resultSlice = append(resultSlice, addr)
}
}
fmt.Println(resultSlice)
The output is what you want.
An empty struct takes no space in monery
What's more, always use camel in golang.

Count similar array value

I'm trying to learn Go (or Golang) and can't seem to get it right. I have 2 texts files, each containing a list of words. I'm trying to count the amount of words that are present in both files.
Here is my code so far :
package main
import (
"fmt"
"log"
"net/http"
"bufio"
)
func stringInSlice(str string, list []string) bool {
for _, v := range list {
if v == str {
return true
}
}
return false
}
func main() {
// Texts URL
var list = "https://gist.githubusercontent.com/alexcesaro/c9c47c638252e21bd82c/raw/bd031237a56ae6691145b4df5617c385dffe930d/list.txt"
var url1 = "https://gist.githubusercontent.com/alexcesaro/4ebfa5a9548d053dddb2/raw/abb8525774b63f342e5173d1af89e47a7a39cd2d/file1.txt"
//Create storing arrays
var buffer [2000]string
var bufferUrl1 [40000]string
// Set a sibling counter
var sibling = 0
// Read and store text files
wordList, err := http.Get(list)
if err != nil {
log.Fatalf("Error while getting the url : %v", err)
}
defer wordList.Body.Close()
wordUrl1, err := http.Get(url1)
if err != nil {
log.Fatalf("Error while getting the url : %v", err)
}
defer wordUrl1.Body.Close()
streamList := bufio.NewScanner(wordList.Body)
streamUrl1 := bufio.NewScanner(wordUrl1.Body)
streamList.Split(bufio.ScanLines)
streamUrl1.Split(bufio.ScanLines)
var i = 0;
var j = 0;
//Fill arrays with each lines
for streamList.Scan() {
buffer[i] = streamList.Text()
i++
}
for streamUrl1.Scan() {
bufferUrl1[j] = streamUrl1.Text()
j++
}
//ERROR OCCURRING HERE :
// This code if i'm not wrong is supposed to compare through all the range of bufferUrl1 -> bufferUrl1 values with buffer values, then increment sibling and output FIND
for v := range bufferUrl1{
if stringInSlice(bufferUrl1, buffer) {
sibling++
fmt.Println("FIND")
}
}
// As a testing purpose thoses lines properly paste both array
// fmt.Println(buffer)
// fmt.Println(bufferUrl1)
}
But right now, my build doesn't even succeed. I'm only greeted with this message:
.\hello.go:69: cannot use bufferUrl1 (type [40000]string) as type string in argument to stringInSlice
.\hello.go:69: cannot use buffer (type [2000]string) as type []string in argument to stringInSlice
bufferUrl1 is an array: [4000]string. You meant to use v (each
string in bufferUrl1). But in fact, you meant to use the second
variable—the first variable is the index which is ignored in the code
below using _.
type [2000]string is different from []string. In Go, arrays and slices are not the same. Read Go Slices: usage and internals. I've changed both variable declarations to use slices with the same initial length using make.
These are changes you need to make to compile.
Declarations:
// Create storing slices
buffer := make([]string, 2000)
bufferUrl1 := make([]string, 40000)
and the loop on Line 69:
for _, s := range bufferUrl1 {
if stringInSlice(s, buffer) {
sibling++
fmt.Println("FIND")
}
}
As a side-note, consider using a map instead of a slice for buffer for more efficient lookup instead of looping through the list in stringInSlice.
https://play.golang.org/p/UcaSVwYcIw has the fix for the comments below (you won't be able to make HTTP requests from the Playground).

Passing reflect.Value to datastore.GetMulti in Google App Engine

I have a wrapper function mypkg.GetStart around datastore.GetMulti. The arguments of the wrapper function must be identical to appengine.GetMulti. I would like to get the first two entities of dst, for the sake of this example. My code currently looks like below but does not work. datastore.GetMulti produces the error datastore: dst has invalid type.
type myEntity struct {
Val Int
}
keys := []*datastore.Key{keyOne, keyTwo, keyThree}
entities := make([]myEntity, 3)
mypkg.GetStart(c, keys, enities)
My mypkg.GetStart code is as follows:
func GetStart(c appengine.Context, keys []*datastore.Key, dst interface{}) error{
v := reflect.ValueOf(dst)
dstSlice := v.Slice(0, 2)
return datastore.GetMulti(c, keys, dstSlice)
}
How can I make this work? Note this is a follow up question to How to sub slice an interface{} that is a slice?
I got this to work by adding Interface() to dstSlice:
func GetStart(c appengine.Context, keys []*datastore.Key, dst interface{}) error{
v := reflect.ValueOf(dst)
dstSlice := v.Slice(0, 2)
return datastore.GetMulti(c, keys, dstSlice.Interface())
}

Resources