Tip for a Beginner Go

I started learning Go today. In particular, I read the documentation, http://blog.go-lang.org and http://golang-book.com .

What is the best way to learn a language than practice? I wrote a small program in Go that gives changes to the client using the least amount of coins. After reading the earlier tutorials, there seem to be several ways to do the same in Go.

For instance:

  • Is it possible to declare multiple variables in one var statement?
  • Should you declare variables with var or deduce a type using: = okay?
  • When declaring a piece of coins, is it best to declare them locally in a function?

package main import "fmt" func main() { fmt.Printf("%v", change(158)) } func change(value int) []int { var ( change []int coins = []int{ 100, 50, 20, 10, 5, 2, 1 } ) for value > 0 { for i := 0; i < len(coins); i++ { if coins[i] <= value { change = append(change, coins[i]) value -= coins[i] break } } } return change } 

Many thanks!

+4
source share
4 answers

Some things are just a matter of style. Also think about what will happen if you are asked to expand a function, for example, support multiple currencies as part of a money package. For example, I would write your change function as a method in a currency:

 package main import "fmt" type Currency struct { Name string Notes []int Coins []int } var ukCurrency = Currency{ Name: "UK Sterling", Notes: []int{5000, 2000, 1000, 500}, Coins: []int{100, 50, 20, 10, 5, 2, 1}, } var usCurrency = Currency{ Name: "US Dollar", Notes: []int{10000, 5000, 2000, 1000, 500, 100}, Coins: []int{25, 10, 5, 1}, } func (c *Currency) Change(amount int) []int { var change []int for _, note := range c.Notes { for note <= amount { change = append(change, note) amount -= note } if amount == 0 { break } } for _, coin := range c.Coins { for coin <= amount { change = append(change, coin) amount -= coin } if amount == 0 { break } } return change } func main() { for _, currency := range []Currency{ukCurrency, usCurrency} { fmt.Println(currency.Name, currency.Change(158)) fmt.Println(currency.Name, currency.Change(4747)) } } 

Output:

 UK Sterling [100 50 5 2 1] UK Sterling [2000 2000 500 100 100 20 20 5 2] US Dollar [100 25 25 5 1 1 1] US Dollar [2000 2000 500 100 100 25 10 10 1 1] 

As a style, I would revise your change function:

 package main import "fmt" var coins = []int{100, 50, 20, 10, 5, 2, 1} func change(amount int) []int { var change []int for _, coin := range coins { for coin <= amount { change = append(change, coin) amount -= coin } if amount == 0 { break } } return change } func main() { fmt.Println(change(158)) } 
+2
source
  • Is it possible to declare multiple variables in one var statement?

Yes

  • Should you declare variables with var or deduce a type using: = ok?

Both are acceptable. The rule of thumb is that if you need to initialize a variable with a value, use: = if not then use var.

  • When declaring a piece of coin, is it best to declare these local in functions?

The answer almost entirely depends on the individual needs of the application. Your sample program is small here to offer any general recommendations. Local variables in general often offer many advantages over global ones, but sometimes a global variable is better suited.

0
source
  • Is it possible to declare multiple variables in one var statement?

Yes, although in my personal experience I have never seen it locally. The var ( ... ) syntax is traditionally reserved for top-level declarations (although I have never seen this in any style guides, this is just my observation).

  • Should you declare variables with var or deduce a type using: = okay?

Usually := excellent. Usually you want to use var with an explicit type when the type is either not obvious, or you want the variable to be something other than the obvious type, or even forced type casting.

For example, let's say you have a variable a , and you want to make a duplicate variable b . Sometimes you can remind the reader that type a , and not just do b := a .

As an example of a β€œdifferent than obvious” case, imagine that you want a 16-bit floating point value. You can do one of the following, but some of them will consider a second cleaner:

 a := float16(1) var a float16 = 1 

Finally, use var , where you only declare without assigning a variable (for example, to use its default value or assign it later):

 `var a int` 
  • When declaring a piece of coins, is it best to declare them locally in a function?

It mainly depends on two factors: is it logically connected with the function and is it bulky enough if declared locally?

Obviously, if the same value is used in other functions, you might want to declare it globally. Sometimes, even if it is used in only one function, it still makes more sense to declare it globally (for example, if it looks like a general package behavior parameter, for example, how many bytes are used for local storage or something like that).

Even if it is logically associated with a local function, if it is a very long value, declaring it locally can significantly clutter the code, which is a good reason for declaring it globally:

func myFunc () {

// do some things

mySlice: = [] string {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10" "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" 24 ", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37 "," 38 "," 39 "," 40 "," 41 "," 42 "," 43 "," 44 "," 45 "," 46 "," 47 "," 48 "49", "50 "," 51 "," 52 "," 53 "," 54 "," 55 "," 56 "," 57 "," 58 "," 59 "," 60 "," 61 "," 62 ", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73" 74 "," 75 ", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "90", "91", "90 "," 100 "," 101 "," 102 "," 103 "," 104 "," 105 "," 106 "," 107 "," 108 "," 109 "," 110 "," 111 ", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123" 126 ", β€œ125”, β€œ126”, β€œ127”, β€œ128”, β€œ129”, β€œ130”, β€œ131”, β€œ132”, β€œ133”, β€œ134”, β€œ135”, β€œ136”, β€œ137” "," 138 "," 139 "," 140 "," 141 "," 142 "," 143 "," 144 "," 145 "," 146 "," 147 "," 148 "149", "150 "," 151 "," 152 "," 153 "," 154 "," 155 "," 156 "," 157 "," 158 "," 159 "," 160 "," 161 "," 162 "," 163 " , "164", "165", "166", "167", "168" 169 "," 170 "," 171 "," 172 "," 173 "," 174 "," 175 "," 176 " , "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", " 189 "," 190 "," 191 "," 192 "," 193 "194", "195", "196", "197", "198", "199", "200", "

// Do some other things.

}

(Note that I did not write this as a block of code because it would not have turned around differently and conveyed how ugly it is.)

0
source

The best advice I can give you: Read the source code for the embedded packages. The source code will help you greatly. Especially when reading some handy features: what do they do to make my life easier? Also pay attention to any files that end in "_test.go", as they will have examples not contained in the documentation.

Is it possible to declare multiple variables in one var statement?

Yes, if they are connected.

Should you declare variables with var or deduce a type using: = okay? When declaring a piece of coin, it’s best to declare these

Or it works. Just be consistent. Sometimes you need to use var to pre-declare a variable when the function already declares one of the variables that you use:

 func Foo() (err error) { ... var n int if n, err = something.Read(buf); err != nil { return } .... } 

And sometimes the name of the variable is not enough to convey what you are doing.

When declaring a piece of coins, is it best to declare them locally in a function?

If it is a constant, declare it outside.

0
source

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


All Articles