dongshenghe1833 2010-02-13 07:50
浏览 50
已采纳

使用要在运行时填充的字符串切片

I feel a little silly as this should be an easy one, however I just started with go and can't figure it out.

package main

import "fmt"

type Question struct {
 q []string
 a []string
}
func (item *Question) Add(q string, a string) {
 n := len(item.q)
 item.q[n] := q
 item.a[n] := a
}

func main() {
 var q Question
 q.Add("A?", "B.")
}

When Compiling it gives the errors:

q.go:11:12: error: expected ';' or '}' or newline
q.go:12:12: error: expected ';' or '}' or newline

that refers to the opening brace of item.q[n] := q and the following line.

I'm certain that I'm using slices incorrectly as it works fine with a simple string instead, but I'm not sure how to fix it.

edit: I have re-implemented it using StringVectors as per Pat Notz's advice and it works well. The following is the working code:

package main

import (
    fmt "fmt"
    vector "container/vector"
)

type Question struct {
    q vector.StringVector
    a vector.StringVector
}
func (item *Question) Add(q string, a string) {
    item.q.Push(q)
    item.a.Push(a)
}
func (item *Question) Print(index int) {
    if index >= item.q.Len() {
        return
    }
    fmt.Printf("Question: %s
Answer: %s
", item.q.At(index), item.a.At(index))
}
func main() {
    var q Question
    q.Add("A?", "B.")
    q.Print(0)
}
  • 写回答

4条回答 默认 最新

  • douzinei6926 2010-02-14 16:39
    关注

    You sidestepped the problems you were having using slices by delegating them to StringVector. I've revised your initial implementation, which used string slices, to become a valid, working program.

    type Question struct {
        q   []string
        a   []string
    }
    

    The type Question is a struct which has two elements, q and a, which are slices of an array of strings. A slice implicitly contains a pointer to the element of the array which begins the slice, the length of the slice, and the capacity of the slice.

    var q Question
    

    declares q, allocating storage for the Question struct, and initializes the struct fields (slices q.q and q.a) to zero i.e. the slice pointers are nil, and the slice len() and cap() functions return zero. No storage is allocated for string arrays; we need to do that separately.

    package main
    
    import "fmt"
    
    type Question struct {
        q   []string
        a   []string
    }
    
    func addString(ss []string, s string) []string {
        if len(ss)+1 > cap(ss) {
            t := make([]string, len(ss), len(ss)+1)
            copy(t, ss)
            ss = t
        }
        ss = ss[0 : len(ss)+1]
        ss[len(ss)-1] = s
        return ss
    }
    
    func (item *Question) Add(q string, a string) {
        item.q = addString(item.q, q)
        item.a = addString(item.a, a)
    }
    
    func main() {
        var q Question
        q.Add("A?", "B.")
        fmt.Println("Q&A", q)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号