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条)

报告相同问题?

悬赏问题

  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。