Learn Go with Head First Go

Chapter 2. which code runs next?: Conditionals and Loops

Aditya Patel
6 min readNov 17, 2022

Methods:

Methods are functions that are associated with values of a particular type.

Example of using methods
Year() is a method which is called on the time.Time value type

The dot indicates that the thing on its right belongs to the thing on its left. Whereas the functions we saw earlier belonged to a package, the methods belong to an individual value.

Multiple return values from a function or method:

ReadString method returns two values input and err
err is also return from ReadString

Handling error:

Option 1: Ignore the return value with the blank identifier

using blank identifier

Go does not allow us to declare a variable unless we use it .

When we have a value that would normally be assigned to a variable, but that we don’t intend to use, we can use Go’s blank identifier.

To use the blank identifier, simply type a single underscore ( _ ) character in an assignment statement where you would normally type a variable name.

Option 2: Handle the error

if there is not err then it’s value will be nil

Also in go if or else if statement does not require () brackets and are same as any other language.

Avoid shadowing name:

In above code we noticed that error is name err not error. The reason is name shadowing because naming a variable error would be a bad idea, because it would shadow the a name of a type called error.

Here is a example if naming variable which are actually types in go
Here we are using int as type and append as function and we are getting errors

To avoid confusion for yourself and your fellow developers, you should avoid shadowing names wherever possible.

give variables a meaningful name

Blocks:

Go code can be divided up into blocks, segments of code. Blocks are usually surrounded by curly braces({}).

Blocks and variable scope:

Each variable you declare has a scope: a portion of your code that it’s “visible” within. A declared variable can be accessed anywhere within its scope, but if you try to access it outside that scope, you’ll get an error.

shows the scope various vars

In Go variables are blocked scoped not function.

Block means curly braces {}

Grading program:

In grading program user gives input and program gives output showing whether the user is passed or failed

Grading program

When we use a short variable declaration only one variable has to be new

At least one variable name in a short variable declaration is should be new which will be treated as declaration and the existing names are treated as an assignment.

The reason for this special handling: a lot of Go functions return multiple values. So it would be really a hassle to declare all the variables separately just because you want to reuse one of them.

Package name vs. import paths:

The math/rand package has a Intn function that can generate a random number.

Why don’t we use math/rand.Intn()

An import path is just a unique string that identifies a package and that you use in an import statement. Once you have imported that package, you can refer to it by it’s package name.

import path vs package name

Loop:

The Go for loop is similar to—but not the same as—C's. It unifies for and while and there is no do-while. There are three forms, only one of which has semicolons.

Parentheses is not required in for loop in GO/

// Like a C for
for init; condition; post { }

// Like a C while
for condition { }

// Like a C for(;;)
for { }

The ++ and — statements are frequently used in loop post statements.

Go also includes the assignment operators += and -= -> like c++ and c. They take the value in a variable, add or subtract another value, and then assign the result back to the variable.

= and -= can be used in a loop to count in increments other than 1.

Loops and scope:

Just like with conditionals, the scope of any variables declared within a loop’s block is limited to that block (although the init statement, condition expression, and post statement can be considered part of that scope as well).

“Continue” and “Break” keyword:

Go provides two keywords that control the flow of a loop.

Continue → immediately skips to the next iteration of a loop.

Break → immediately breaks out of a loop.

Bullet Points:

  • A method is a kind of function that’s associated with values of a given type.
  • Go treats everything from a // marker to the end of the line as a comment — and ignores it.
  • Multiline comments start with /* and end with */. Everything in between, including newlines, is ignored.
  • It’s conventional to include a comment at the top of every program, explaining what it does.
  • Unlike most programming languages, Go allows multiple return values from a function or method call.
  • One common use of multiple return values is to return the function’s main result, and then a second value indicating whether there was an error.
  • To discard a value without using it, use the _ blank identifier. The blank identifier can be used in place of any variable in any assignment statement.
  • Avoid giving variables the same name as types, functions, or packages; it causes the variable to shadow (override) the item with the same name.
  • Functions, conditionals, and loops all have blocks of code that appear within {} braces.
  • Their code doesn’t appear within {} braces, but files and packages also comprise blocks.
  • The scope of a variable is limited to the block it is defined within, and all blocks nested within that block.
  • In addition to a name, a package may have an import path that is required when it is imported.
  • The continue keyword skips to the next iteration of a loop.
  • The break keyword exits out of a loop entirely.

This story is meant as a personal note

--

--