新的未定义向量

huangapple go评论97阅读模式
英文:

undefined vector.New

问题

从这个问题中,我尝试创建一个新的向量,但编译器说它未定义:

$ 6g -V
6g版本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,"\n");
 }

}
.
$ 6g vectest.go
vectest.go:7: undefined: vector.New

可能出了什么问题?

英文:

From this question

https://stackoverflow.com/questions/1726336/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 &gt; vectest.go &lt;&lt;.
&gt; package main
&gt; 
&gt; import vector &quot;container/vector&quot;
&gt; import &quot;fmt&quot;
&gt; 
&gt; func main() {
&gt;      vec := vector.New(0);
&gt;      buf := make([]byte,10);
&gt;      vec.Push(buf);
&gt; 
&gt;      for i := 0; i &lt; vec.Len(); i++ {
&gt;      el := vec.At(i).([]byte);
&gt;      fmt.Print(el,&quot;\n&quot;);
&gt;      }
&gt; }
&gt; .
$ 6g vectest.go 
vectest.go:7: undefined: vector.New

What might be wrong ?

答案1

得分: 7

我改进了你的convertToLCD代码以提高性能:5,745 ns/op对比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] = '\n'
			}
		}
	}
	return string(display)
}

func main() {
	fmt.Printf("%s\n", convertToLCD(1234567890))
}

输出:

    _  _     _  _  _  _  _  _ 
  | _| _||_||_ |_   ||_||_|| |
  ||_  _|  | _||_|  ||_| _||_|
英文:

> 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 (
	&quot;fmt&quot;
	&quot;strconv&quot;
)

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-&#39;0&#39;) * lcdWidth
		for line := 0; line &lt; 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] = &#39;\n&#39;
			}
		}
	}
	return string(display)
}

func main() {
	fmt.Printf(&quot;%s\n&quot;, convertToLCD(1234567890))
}

Output:

    _  _     _  _  _  _  _  _ 
  | _| _||_||_ |_   ||_||_|| |
  ||_  _|  | _||_|  ||_| _||_|

答案2

得分: 2

这是真的,在r60.3中没有vector.New函数,但是与其修补这段代码,你应该学习新的append函数。它使得vector包变得不再必要,实际上这个包在一段时间前就从每周发布中移除了。

英文:

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.

huangapple
  • 本文由 发表于 2012年3月20日 07:15:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/9779332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定