英文:
What is "A Tour of Go" trying to say?
问题
在教程中有一些地方,如果你不了解的话,会让你感到茫然或者没有链接。所以对于这些内容的长度我很抱歉:
也尝试打印 needInt(Big)
我猜测整数允许的位数比常量少?
{ } 是必需的。
(听起来熟悉吗?)
这暗示了哪种语言?
(而类型声明会做你所期望的事情。)
为什么我们需要 type
这个词和 struct
这个词?我应该期望什么?
为什么构造函数中有隐式的零值?这听起来像是 Go 的一个危险的设计选择。除了 http://golang.org/doc/go_faq.html,还有其他的 PEP 或者什么吗?
Make
?有构造函数吗?new
和 make
有什么区别?
delete
是从哪里来的?我没有导入它。
%v
格式化符号代表什么?值?
panic: runtime error: index out of range
goroutine 1 [running]:
tour/pic.Show(0x400c00, 0x40ca61)
go/src/pkg/tour/pic/pic.go:24 +0xd4
main.main()
/tmpfs/gosandbox-15c0e483_5433f2dc_ff6f028f_248fd0a7_d7c2d35b/prog.go:14 +0x25
我猜我搞砸了 Go....
package main
import "tour/pic"
func Pic(dx, dy int) [][]uint8 {
image := make([][]uint8, 10)
for i := range image {
image[i] = make([]uint8, 10)
}
return image
}
func main() {
pic.Show(Pic)
}
当函数失败时,我返回错误值?我必须在每个函数调用中都加上错误检查吗?当我写疯狂的代码时,程序的流程不会中断吗?例如 Copy(only_backup, elsewhere);Delete(only_backup)
,如果 Copy 失败....
为什么他们会设计成这样?
英文:
There are a few points in the tutorial that sort of leave you on your own without a clue or link if you're not in the know I guess. So I'm sorry about the length of these:
Try printing needInt(Big) too
I'm guessing ints are allowed less bits than constants?
the { } are required.
(Sound familiar?)
Which language is alluded to?
(And a type declaration does what you'd expect.)
Why do we need the word type
and the word struct
? What was I supposed to expect?
Why implicit zeroes in the constructor? This sounds like a dangerous design choice by Go. Is there a PEP or anything beyond http://golang.org/doc/go_faq.html on this?
Make
? Are there constructors? What's the difference between new
and make
?
Where did delete
come from? I didn't import it.
What's the %v
formatter stand for? Value?
panic: runtime error: index out of range
goroutine 1 [running]:
tour/pic.Show(0x400c00, 0x40ca61)
go/src/pkg/tour/pic/pic.go:24 +0xd4
main.main()
/tmpfs/gosandbox-15c0e483_5433f2dc_ff6f028f_248fd0a7_d7c2d35b/prog.go:14 +0x25
I guess I broke go somehow....
package main
import "tour/pic"
func Pic(dx, dy int) [][]uint8 {
image := make([][]uint8, 10)
for i := range image {
image[i] = make([]uint8, 10)
}
return image
}
func main() {
pic.Show(Pic)
}
I return error values when a function fails? I have to qualify every single function call with an error check? The flow of the program is uninterrupted when I write crazy code? E.g. Copy(only_backup, elsewhere);Delete(only_backup)
and Copy fails....
Why would they design it like that?
答案1
得分: 18
- #15:
是的,确切地说。根据规范,“数值常量表示任意精度的值,不会溢出”,而type int
有32位或64位。
- #21:
没有,它暗指#16,它以相同的措辞关于for
循环说了同样的事情。
- #25 :
“类型声明做你期望的事情”有点不幸,我同意(因为它对读者的期望假设太多...),但它意味着你正在定义一个结构体(使用struct
关键字)并将类型名称“Vertex”绑定到它,使用type Vertex
部分(参见http://golang.org/ref/spec#Type_declarations)。
- #28:
未初始化的结构体被清零的事实在许多情况下非常有用(许多标准结构体如缓冲区也使用它)
它不仅仅是在构造函数中隐含的。看看这个
var i int; fmt.Println(i)
这将打印出0
。这类似于像Java这样的语言,原始类型具有隐式默认值。布尔值为false,整数为零,等等。关于零值的规范。
- #30:
new
分配内存并返回指向它的指针,而make
是一个专门用于切片、映射和通道的函数。参见http://golang.org/doc/effective_go.html#allocation_new,了解更详细的make
与new
的解释。
- #33:
delete
,像append
或copy
一样,是该语言的基本运算符之一。请参阅完整的运算符列表:http://golang.org/ref/spec#Predeclared_identifiers
- #36:
是的,%v
代表“值”。请参阅http://golang.org/pkg/fmt/
- #47:
试试这个:
func Pic(dx, dy int) [][]uint8 {
image := make([][]uint8, dy) // dy,不是10
for x := range image {
image[x] = make([]uint8, dx) // dx,不是10
for y := range image[x] {
image[x][y] = uint8(x*y) //让我们尝试其中一个提到的“有趣函数”
}
}
return image
}
- #59:
该语言的设计和约定鼓励您在发生错误时显式检查错误(与其他语言中抛出异常并有时捕获异常的约定不同)。在某些情况下,这使得Go代码冗长,但幸运的是,有一些技术可以用来最小化重复的错误处理。
(引用自错误处理和Go)
英文:
- #15:
> I'm guessing int's are allowed less bits than constants?
Yes, exactly. According to the spec, "numeric constants represent values of arbitrary precision and do not overflow", whereas type int
has either 32 or 64 bits.
- #21:
> Which language is alluded to?
None; it's alluding to #16, which says the same thing, in the same words, about for
-loops.
- #25 :
a type declaration does what you'd expect
is a little unfortunate, I agree (as it assumes too much on what a reader could expect...) but it means you're defining a struct (with the struct
keyword) and binding the type name "Vertex" to it, with the type Vertex
part (see http://golang.org/ref/spec#Type_declarations)
- #28:
the fact that uninitialized structs are zeroed is really really useful in many cases (many standard structs like buffers use it also)
It's not implicit in the contructor only. Look at this
var i int; fmt.Println(i)
This prints out 0
. This is similar to something like java where primitive types have an implicit default value. booleans are false, integers are zero, etc. The spec on zero values.
- #30:
new
allocates memory and returns a pointer to it, while make
is a special function used only for Slices, maps and channels.
See http://golang.org/doc/effective_go.html#allocation_new for a more in-depth explanation of make
vs new
- #33:
delete
, like append
or copy
is one of the basic operators of the language. See the full list of them at: http://golang.org/ref/spec#Predeclared_identifiers
- #36:
Yes, %v
stands for "value". See http://golang.org/pkg/fmt/
- #47:
try with this:
func Pic(dx, dy int) [][]uint8 {
image := make([][]uint8, dy) // dy, not 10
for x := range image {
image[x] = make([]uint8, dx) // dx, not 10
for y := range image[x] {
image[x][y] = uint8(x*y) //let's try one of the mentioned
// "interesting functions"
}
}
return image
}
-
#59:
> The language's design and conventions encourage you to explicitly
> check for errors where they occur (as distinct from the convention in
> other languages of throwing exceptions and sometimes catching them).
> In some cases this makes Go code verbose, but fortunately there are
> some techniques you can use to minimize repetitive error handling.(quoted from Error handling and Go )
答案2
得分: 3
是的,数值常量是高精度值。任何语言中的int
类型都没有其他数值类型那么精确。
不清楚是指哪种语言,但它与C和Java相反,C和Java中需要使用( )
,而{ }
是可选的。
为什么我们需要type
和struct
这两个词?我应该期望什么?
如果你熟悉C,那么它的行为符合你的期望。
为什么构造函数中有隐式的零值?
这不仅仅是在构造函数中隐式的。看看下面的例子:
var i int
fmt.Println(i)
这会打印出0
。这类似于Java中的原始类型具有隐式默认值的情况。布尔值为false,整数为零,等等。
make
函数是用来做什么的?有构造函数吗?new
和make
有什么区别?
make
函数接受额外的参数来初始化数组、切片或映射的大小。而new
只是返回一个指向类型的指针。
type Data struct {}
// d1和d2都是指针
d1 := new(Data)
d2 := &Data{}
至于是否有构造函数,只有在你自己创建和引用它们时才有。下面是在Go中通常实现构造函数的方式:
type Data struct {}
func NewData() *Data {
return new(Data)
}
%v
格式化符代表什么?代表值吗?
是的。
当函数失败时,我返回错误值?...为什么设计成这样?
起初我也有同样的感觉。但我的观点已经改变了。你可以忽略标准库中的错误,如果你愿意,也可以不去处理它,但是一旦你掌握了它,我个人发现我有更好(也更可读)的错误检查。
我可以说的是,当我做错时,感觉就像是重复的错误处理,感觉是不必要的。当我最终开始正确处理时...嗯,就是我上面说的那样。
英文:
I'm guessing int's are allowed less bits than constants?
yes, Numeric constants are high-precision values. An int
in any language doesn't have near the precision of other numeric types.
Which language is alluded to?
No clue but it is backwards from C and Java where ( )
is required and { }
is optional.
Why do we need the word type and the word struct? What was I supposed to expect?
If you're familiar with C, then it does what you'd expect.
Why implicit zeroes in the constructor?
It's not implicit in the contructor only. Look at this
var i int
fmt.Println(i)
This prints out 0
. This is similar to something like java where primitive types have an implicit default value. booleans are false, integers are zero, etc.
Make? Are there constructors? What's the difference between new and make?
make
accepts additional parameters for initializing the size of an array, slice, or map. new
on the other hand just returns a pointer to a type.
type Data struct {}
// both d1 and d2 are pointers
d1 := new(Data)
d2 := &Data{}
As for are there constructors?
, only if you make and reference them. This how one normally implements a constructor in Go.
type Data struct {}
func NewData() *Data {
return new(Data)
}
What's the %v formatter stand for? Value?
Yep
I return error values when a function fails? ... Why would they design it like that?
I felt the same way at first. My opinion has changed though. You can ignore errors from the std library if you like and not bother with it yourself, but once I had a handle on it, I personally find I have better (and more readable) error checking.
What I can say is when I was doing it wrong, it felt like repetitive error handling that felt unnecessary. When I finally started doing it right... well, what I just said above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论