douzai9405 2016-04-01 00:14
浏览 43

GO:数组/切片为常规字符串

I am sort of new to golang, and also kind of new to programming. And go has been very hard for me. This is one thing that always confuses me: data types. If you run this(not on the playground) then it will spit out:

./main.go:40: cannot use recorded (type string) as type SVC in append

and if I reverse the values in the append call, it will spit out:

./main.go:40: first argument to append must be slice; have string

What I am trying to do is grab all of the stuff in the home directory, append all of the values with the modifications to an array, then put the array into a file using ioutil. All I want(as of now) is to append the values to the slice in func record. Can anybody help?

package main

import "os"
import "io/ioutil"
import "fmt"

type SVC struct {
        key string
        val string
}

func main() {
        os.Chdir("../../../../../../..")
        var data, err = ioutil.ReadDir("home")
        checkerr(err)
        for _, data := range data {
                fmt.Println(data.Name())
        }
        os.Chdir("home/beanboybunny/repos/bux/go/src/bux")
}
func checkerr(err1 error) {
        if err1 != nil {
                fmt.Println("error")
        }
}
func writer(dataname string) {
        f := "var "
        uname := dataname
        q := " = VVC{
"
        w := "  bux: 1,
"
        e := "  op: true,
"
        c := "}" 

        b2 := f + uname + q + w + e + c 
        record(b2)
}

func record(recorded string) {
        cache := []SVC{}
        record SVC := recorded 
        appenda := append(cache, recorded)
}
  • 写回答

2条回答 默认 最新

  • dongqie4233 2016-04-01 00:21
    关注

    Your type SVC struct has two private string fields. If you just want an array of strings you don't need the SVC type.

    If you're just trying to build an array of strings with your specially formatted transform of the items in the home directory, here is a start showing the use of append and format strings: https://play.golang.org/p/eUKTKRxwfp

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "os"
        "strings"
    )
    
    func main() {
        os.Chdir("../../../../../../..")
        var data, err = ioutil.ReadDir("home")
        checkerr(err)
        lines := []string{}
        for _, data := range data {
            fmt.Println(data.Name())
            lines = append(lines, buildLine(data.Name()))
        }
        fmt.Println(strings.Join(lines, "
    "))
        os.Chdir("home/beanboybunny/repos/bux/go/src/bux")
    }
    
    func checkerr(err1 error) {
        if err1 != nil {
            fmt.Printf("error: %v", err1)
        }
    }
    
    func buildLine(dataname string) string {
        return fmt.Sprintf("var %s = VVC{
     bux: 1,
     op: true,
    }", dataname)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集