ledongthuc/goterators

Try to use filter example results in error

rufreakde opened this issue · 7 comments

Hi,
I imported the lib and also did go mod tidy all good but when I just try to filter anything:

	list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

	filteredItems, err := goterators.Filter(list, func(item int) bool {
		return item % 2 == 0
	})

i get:
Screenshot 2022-08-15 at 10 51 31

I just copied the example. Is this a bug @ledongthuc?

Hi @rufreakde, first of all, thanks for your report.
It's an issue of documentation, the code should be:

	list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

	filteredItems := goterators.Filter(list, func(item int) bool {
		return item % 2 == 0
	})

The filter function won't return an error. So could you try again without a return error?

I will appreciate it if you can help to update the README document to correct it, or I will update it later this weekend.

Seemed not to help:
Screenshot 2022-08-15 at 11 22 02

My code looks like this:

	list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

	filteredItems := goterators.Filter( list, func(item int) bool {
		return item % 2 == 0
	})

go version:
go version go1.18.5 darwin/amd64

this is how the definition looks like:

package goterators

// Filter return items that pass the filter function.
func Filter[T any](source []T, filteredFunc func(item T) bool) (output []T) {
	for _, item := range source {
		if filteredFunc(item) {
			output = append(output, item)
		}
	}
	return output
}

Dont know why it thinks the parameters are not correct?

It works from my side, with go version 1.18. My code:

package main

import (
        "fmt"

        "github.com/ledongthuc/goterators"
)

func main() {
        list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

        filteredItems := goterators.Filter(list, func(item int) bool {
                return item%2 == 0
        })
        fmt.Println("list: ", list)
        fmt.Println("filteredItems: ", filteredItems)
}

Screenshot 2022-08-15 at 12 07 35

But I think something is wrong with your IDE that can be configured to a lower version.

Very strange indeed i tested it as well and go version 18 works it seems to be an issue appearing with the plugin
https://marketplace.visualstudio.com/items?itemName=golang.Go

It is the first time I experienced this kind of issue.

Screenshot 2022-08-15 at 14 33 04

.

ok, I don't have much experience in the visual studio and its plugin. But let's try a few troubleshooting steps:

  • Close and reopen VisualStudio again, and test if it works
  • Uninstall and re-install the plugin, and test if it works
  • And finally, uninstall and reinstall the visual studio, and test if it works again.

Sorry because I'm using VIm.

Tried seems not to work. Would be nice to see if someone else using VSCODE is having a similar issue.
Could hinder a wider adoption of the lib. I will try to find a colleague to test it on their system.

Ok, it will be good. I think the ticket is solved. But let's keep it open and I remember to update README.
Again, thanks for your report.