登录
首页 >  Golang >  Go问答

如何将 MapSet 转换为切片?

来源:stackoverflow

时间:2024-02-28 20:54:27 414浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《如何将 MapSet 转换为切片?》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我正在阅读 donovan 的“the go 编程语言”一书,并尝试实现一个练习,从多个文件及其出现的文件中打印重复行:

package main

import (
    "fmt"
    "io/ioutil"
    "os"
    "strings"

    mapset "github.com/deckarep/golang-set"
)

func main() {
    counts := make(map[string]int)
    occurrences := make(map[string]mapset.set)
    for _, filename := range os.args[1:] {
        data, err := ioutil.readfile(filename)
        if err != nil {
            fmt.fprintf(os.stderr, "dup3: %v\n", err)
            continue
        }
        for _, line := range strings.split(string(data), "\n") {
            counts[line]++
            occurrences[line].add(filename)
        }
    }
    for line, n := range counts {
        if n > 1 {
            fmt.printf("%d\t%s\t%s\n", n, line, strings.join(occurrences[line], ", "))
        }
    }
}

为了完成这个练习,我使用了 https://godoc.org/github.com/deckarep/golang-set 包。但是,我不确定如何打印出由“,”连接的集合的元素。通过这段代码,我得到了

./hello.go:23:30: first argument to append must be slice; have interface { Add(interface {}) bool; Cardinality() int; CartesianProduct(mapset.Set) mapset.Set; Clear(); Clone() mapset.Set; Contains(...interface {}) bool; Difference(mapset.Set) mapset.Set; Each(func(interface {}) bool); Equal(mapset.Set) bool; Intersect(mapset.Set) mapset.Set; IsProperSubset(mapset.Set) bool; IsProperSuperset(mapset.Set) bool; IsSubset(mapset.Set) bool; IsSuperset(mapset.Set) bool; Iter() <-chan interface {}; Iterator() *mapset.Iterator; Pop() interface {}; PowerSet() mapset.Set; Remove(interface {}); String() string; SymmetricDifference(mapset.Set) mapset.Set; ToSlice() []interface {}; Union(mapset.Set) mapset.Set }
./hello.go:28:64: cannot use occurrences[line] (type mapset.Set) as type []string in argument to strings.Join

不过,我无法轻松找到如何将 set 转换为切片。知道我如何实现这个目标吗?


解决方案


xy 问题询问的是您尝试的解决方案,而不是您的实际问题:The XY Problem

The Go Programming Language by Alan A. A. Donovan and Brian W. Kernighan,练习 1.4 旨在使用 go 地图。

例如,

// Modify dup3 to print the names of all files in which each duplicated line occurs.
package main

import (
    "fmt"
    "io/ioutil"
    "os"
    "strings"
)

func main() {
    // counts = [line][file]count
    counts := make(map[string]map[string]int)
    for _, filename := range os.Args[1:] {
        data, err := ioutil.ReadFile(filename)
        if err != nil {
            fmt.Fprintf(os.Stderr, "Exercise 1.4: %v\n", err)
            continue
        }
        for _, line := range strings.Split(string(data), "\n") {
            files := counts[line]
            if files == nil {
                files = make(map[string]int)
                counts[line] = files
            }
            files[filename]++
        }
    }
    for line, files := range counts {
        n := 0
        for _, count := range files {
            n += count
        }
        if n > 1 {
            fmt.Printf("%d\t%s\n", n, line)
            for name := range files {
                fmt.Printf("%s\n", name)
            }
        }
    }
}

到这里,我们也就讲完了《如何将 MapSet 转换为切片?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>