Count similar array values

So here is the deal, 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.

What I’m trying to do is the 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 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

My build doesn’t even succeed.

As I said I’m new to go and not familiar with languages like those, I mostly write in PHP.

Any hint or explanation on what is happening would be appreciated.

Thanks in advance.

Your use of arrays (the statically sized [2000]string etc) is unusual and confuses you when you try to pass them to functions expecting a slice ([]string). You most likely want to use slices in both cases and use the append function when reading words from the files. Read up on the difference between arrays and slices, as this differs somewhat compared to most other languages. You almost always want slices and not arrays.

The other error is just that you’re passing the whole array to the function when you ought to be passing just the element in the current iteration.

Hey @Nirnae, I’ve just had a look at your question.

I’ve just quickly written this code for you, in a similarly structured manner to the way your code was written, for you to be able to better grasp what wasn’t working with your code before.

Let me know if you have any questions.

package main

import (
	"bufio"
	"fmt"
	"log"
	"net/http"
)

// Create 2 strings that contain the URL's of 2 text files.
var listURL = "https://gist.githubusercontent.com/alexcesaro/c9c47c638252e21bd82c/raw/bd031237a56ae6691145b4df5617c385dffe930d/list.txt"
var fileURL = "https://gist.githubusercontent.com/alexcesaro/4ebfa5a9548d053dddb2/raw/abb8525774b63f342e5173d1af89e47a7a39cd2d/file1.txt"

func main() {
	// Get the list URL's page.
	listPage, err := http.Get(listURL)
	if err != nil {
		log.Fatalln(err)
	}
	defer listPage.Body.Close()

	// Create a string slice to store all of the words from the list page's body.
	var listPageWords []string

	// Create a new scanner for the list page's body
	scanner := bufio.NewScanner(listPage.Body)

	// Set the split function for the scanner to scan individual words.
	scanner.Split(bufio.ScanWords)

	// Scan each word from the list page's body.
	for scanner.Scan() {
		// Append each word to the `listPageWords` slice.
		listPageWords = append(listPageWords, scanner.Text())
	}

	// Make sure there were no errors scanning the file's words above.
	if err := scanner.Err(); err != nil {
		log.Fatalln(err)
	}

	// Now get the file URL's page.
	filePage, err := http.Get(fileURL)
	if err != nil {
		log.Fatalln(err)
	}
	defer filePage.Body.Close()

	// Create a new string slice to store only the words that are present in both files.
	var presentInBoth []string

	// Re-use the scanner but set it to scan from the file page's body instead now.
	scanner = bufio.NewScanner(filePage.Body)

	// Re-set the scanner to scan words from the file page's body
	scanner.Split(bufio.ScanWords)

	// Now scan each word from the file page's body.
	for scanner.Scan() {
		// Check if the current word is stored in the `listPageWords` string slice
		// and if so, append it to the `presentInBoth` string slice.
		if stringInSlice(scanner.Text(), listPageWords) {
			presentInBoth = append(presentInBoth, scanner.Text())
		}
	}

	// Print out all of words present in both text files.
	// for _, word := range presentInBoth {
	// 	fmt.Println(word)
	// }

	// Print out the amount of shared words between the 2 text files.
	fmt.Println(len(presentInBoth))
}

// stringInSlice checks whether a string exists in a string slice or not.
func stringInSlice(str string, list []string) bool {
	for _, v := range list {
		if v == str {
			return true
		}
	}
	return false
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.