dongsunny1113 2019-09-08 23:11
浏览 59
已采纳

如何从mapset.Set制作切片?

I'm reading Donovan's "The Go Programming Language" book and trying to implement an exercise which prints duplicate lines from several files and the files in which they occur:

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
", err)
            continue
        }
        for _, line := range strings.Split(string(data), "
") {
            counts[line]++
            occurrences[line].Add(filename)
        }
    }
    for line, n := range counts {
        if n > 1 {
            fmt.Printf("%d\t%s\t%s
", n, line, strings.Join(occurrences[line], ", "))
        }
    }
}

To accomplish the exercise, I've used the https://godoc.org/github.com/deckarep/golang-set package. However, I'm not sure how to print out the elements of the set joined by a ", ". With this code, I get a

./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

I wasn't able to easily find out how to convert the Set to a slice though. Any idea how I might accomplish this?

  • 写回答

1条回答 默认 最新

  • dsf23223 2019-09-09 14:10
    关注

    The XY problem is asking about your attempted solution rather than your actual problem: The XY Problem.


    The Go Programming Language by Alan A. A. Donovan and Brian W. Kernighan, Exercise 1.4 is designed to use Go maps.

    For example,

    // 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
    ", err)
                continue
            }
            for _, line := range strings.Split(string(data), "
    ") {
                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, line)
                for name := range files {
                    fmt.Printf("%s
    ", name)
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行