What is a "Walk along the path" trying to say?

There are several points in the textbook that leave you without a hint or link, if you do not know, I think. Therefore, I regret their length:

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

Try printing needInt(Big) too 

I assume ints are allowed fewer bits than constants?


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

 the { } are required. (Sound familiar?) 

What language are they referring 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 should I expect?


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

Why are implicit zeros in the constructor? It sounds like a dangerous Go design choice. Is there a PEP or anything beyond http://golang.org/doc/go_faq.html on this?


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

Make ? Are there any constructors? What is the difference between new and Make ?


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

Where did delete come from? I did not import it.


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

What does the %v formatter mean? 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 think I'm broke, 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

Am I returning error values ​​when a function fails? Should I qualify every function call with error checking? Is the program flow interrupted when I write crazy code? For instance. Copy(only_backup, elsewhere);Delete(only_backup) and copy failed ...

Why did they design it like this?


+4
source share
2 answers

try the following:

 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 try one of the mentioned // "interesting functions" } } return image } 
  • # 59 :

    The language and conventions encourage you to explicitly check for errors where they occur (unlike conventions, they throw exceptions into other languages ​​and sometimes catch them). In some cases, this makes Go code long, but fortunately there are some methods you can use to minimize repetitive error handling.

    (quote from Error Handling and Transition )

+16
source

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

yes, numerical constants are high precision values. int in any language does not have the precision of other numeric types.

Which language is alluded to?

There is no hint, but it is inverse 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 are familiar with C, then it does what you expect.

Why implicit zeroes in the constructor?

This is not implied only in the constructor. Look at this

 var i int fmt.Println(i) 

Prints 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 the difference between new and make?

make accepts additional parameters to initialize the size of the 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{} 

How for are there constructors? only if you do and reference them. This, as usual, implements the constructor in Go.

 type Data struct {} func NewData() *Data { return new(Data) } 

What the %v formatter stand for? Value?

Yes

I return error values when a function fails? ... Why would they design it like that?

At first I felt the same way. My opinion has changed. You can ignore errors from the std library if you want, and do not worry about it yourself, but as soon as I got a pen, I personally found that I had a better (and more readable) error check.

What can I say, when I did it wrong, it seemed to me that this was a repeated error handling that was not needed. When I finally started doing it right ... well, what I just said above.

+3
source

Source: https://habr.com/ru/post/1435500/


All Articles