英文:
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 > 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
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 (
"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))
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论