试图说什么“去旅行”? [关闭]

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:

http://tour.golang.org/#15

Try printing needInt(Big) too

I'm guessing ints are allowed less bits than constants?


http://tour.golang.org/#21

the { } are required.

(Sound familiar?)

Which language is alluded to?


http://tour.golang.org/#25

(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?


http://tour.golang.org/#28

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?


http://tour.golang.org/#30

Make? Are there constructors? What's the difference between new and make?


http://tour.golang.org/#33

Where did delete come from? I didn't import it.


http://tour.golang.org/#36

What's the %v formatter stand for? Value?


http://tour.golang.org/#47

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

http://tour.golang.org/#59

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?


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 )

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.