duansaxf095988
duansaxf095988
2012-03-19 23:15

未定义向量

  • vector
  • undefined
已采纳

From this question

How do I use a (generic) vector in go?

I tried to create a new vector but the compiler says it is undefined:

$ 6g -V
6g version release.r60.3 9516
$ cat > vectest.go <<.
> package main
> 
> import vector "container/vector"
> import "fmt"
> 
> func main() {
>      vec := vector.New(0);
>      buf := make([]byte,10);
>      vec.Push(buf);
> 
>      for i := 0; i < vec.Len(); i++ {
>      el := vec.At(i).([]byte);
>      fmt.Print(el,"
");
>      }
> }
> .
$ 6g vectest.go 
vectest.go:7: undefined: vector.New

What might be wrong ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doukanwa6872 doukanwa6872 9年前

    weekly.2011-10-18

    The container/vector package has been deleted. Slices are better. SliceTricks: How to do vector-esque things with slices.

    I revised your convertToLCD code to have better performance: 5,745 ns/op versus 19,003 ns/op.

    package main
    
    import (
        "fmt"
        "strconv"
    )
    
    const (
        lcdNumerals = `
     _     _  _     _  _  _  _  _ 
    | |  | _| _||_||_ |_   ||_||_|
    |_|  ||_  _|  | _||_|  ||_| _|
    `
        lcdWidth   = 3
        lcdHeight  = 3
        lcdLineLen = (len(lcdNumerals) - 1) / lcdWidth
    )
    
    func convertToLCD(n int) string {
        digits := strconv.Itoa(n)
        displayLineLen := len(digits)*lcdWidth + 1
        display := make([]byte, displayLineLen*lcdHeight)
        for i, digit := range digits {
            iPos := i * lcdWidth
            digitPos := int(digit-'0') * lcdWidth
            for line := 0; line < lcdHeight; line++ {
                numeralPos := 1 + lcdLineLen*line + digitPos
                numeralLine := lcdNumerals[numeralPos : numeralPos+lcdWidth]
                displayPos := displayLineLen*line + iPos
                displayLine := display[displayPos : displayPos+lcdWidth]
                copy(displayLine, string(numeralLine))
                if i == len(digits)-1 {
                    display[displayLineLen*(line+1)-1] = '
    '
                }
            }
        }
        return string(display)
    }
    
    func main() {
        fmt.Printf("%s
    ", convertToLCD(1234567890))
    }
    

    Output:

        _  _     _  _  _  _  _  _ 
      | _| _||_||_ |_   ||_||_|| |
      ||_  _|  | _||_|  ||_| _||_|
    
    点赞 评论 复制链接分享
  • dqqyp90576 dqqyp90576 9年前

    It's true there is no vector.New in r60.3, but rather than patch up this code, you should learn the new append function. It made the vector package unnecessary, and in fact the package was removed some time ago from the weekly releases.

    点赞 评论 复制链接分享