- Here you are trying learn something, while here your brain is, doing you a favor by making sure the learning doesn't stick. Your brain thinking, Better leave room for more important things
- Ready, set, Go!
- The Go Playground
- What does it all mean?
- Calling functions
- Using functions from other packages
- Function return values
- Strings
- Runes
- Booleans
- Numbers
- Types
- Declaring variables
- Zero values
- Short variable declarations
- Naming rules
- Conversions
- Installing Go on your computer
- Compiling Go code
- Calling methods
- Multiple return values from a function or method
- Option 1: Ignore the error return value with the blank identifier
- Option 2: Handle the error
- Conditionals
- Avoid shadowing names
- Blocks
- Blocks and variable scope
- Only one variable in a short variable declaration has to be new
- Package names vs. import paths
- Loops
- Init and post statements are optional
- Create guessing game
- Formatting output with Printf and Sprintf
- Declaring functions
- Functions and variable scope
- Error values
- Function parameters receive copies of the arguments
- Pointers
- Pointer types
- Using pointers with functions
Back in 2007, the seach engine google had a problem,maintain programs with milions of lines of code that became very difficult, they had to compile the code into a runnable from, a process which at the time took the an hour, that start idea of create language of large scale application
- Fast compilation
- Less cumbersome code
- Unused memory freed automatically (garbage collection)
- Easy to write software that does several operations simultaneously
- Good support for processors with multiple cores
One the cool feature about go is it can help you handle many users connecting at once. And no matter what youre writting, it will help you ensure that your code is easier to maintain and add to.
The easiest way to try Go is to visit http://play.golang.org in your web browser that simple editor where you can enter Go code (Of course this only works if you have a stable internet connection) and its just for simple test and good enough for starting with Go
Every Go file starts with a package clause. A Package is a collection of code that does similar thing, like formatting strings or drawing images.
The special package its main which is required if this code is going to be run directly (usually from the terminal)
Next, Go files almost always have one or more imports statements. Each file needs to import other packages before its code can use the code those other packages contain.
In other word instead you specify only the packages you need by import them
The last part of every Go file is the actual code, which os often split up into one or more functions. A fucntion is a group of one or more lines of code that you can call(run)
Function named main is special and thats a entry point fucntion and you can call another function inside of that
Recap
- The packages clause
- Any import statements
- The actual code
To call functions, type the function name sth like this: Println() and a pair of paratheses. Println can take one or more arguments: values and also Println can be called with no arguments and that acutually print a empty new line
Once we have imported the packages. we can access any fucntion it offers by typing the package name, a dot, and the name of the fucntion we want
For example fmt.Println()
Notice the function start with Capital letter that becuase of the nature of accessing the Go lang if you declare a function with capital letter that fucntion of fields are accessible from another package if you declare as lower case that fucntion just belong to only that package
Consider this example
package main
import (
"math"
"strings"
)
func main(){
math.Floor(2.67)
strings.Title("head first go")
}
When we call the fmt.Prrintln function, we don't need to communicate with it any further after that. We pass one or more value for Println to print, and we trust that it printed But some times a program needs to be abel to call a function and get data back functions in most programming language can have return values: avalue that the function computes and returns to its caller.
A string is a series of bytes that usually represent text characters. You can define string directly within your code using string literals: text between double quotes
Within strings, characters like newlines, tabs, and other charaters that would be hard to include program code can be represented with escape sequences: a backslash followed by charters that represent another character.
\n --> A newline charater \t --> A tab charater \" --> Double quotation marks \\ --> A backslash
Whereas strings are usually used to represent a whole series of text characters, Go runes are used to represnt single characters
String literals are written surrounded by double quotation marks ("), but rune literals are written with single quotation marks (')
Go uses the Unicode standard for storing runes. Runes are kept as numeric codes, not the charcter themselves, if you print them you get numeric code in the output like A represent 65 or b represent 66 and etc...
Booleans values can be one of only values: true or false. They are especially useful with conditional statements
You can also define numbers directly in Go Go treat integers and floating point numbers as different types, so remember that a decimal point can be used to distinguish an integer from a floating-point number
Go basic math operators work just like they do in most other languages. The + symbol is for addition, - for subtraction, * for multiplication and / for division
< and > use to compare two values and see if one is less then or greater thatn another. You can use == (thats two equal signs) to see if two values are equal,!= use of checking no equal, <= check for less and equal and last but not least >= check for greater and equal
We saw the math.Floor function earlier, which rounds a floating-point number down to the nearest whole number, and also strings.Title function which converts a string to title case but waht would happen if you pass string to Floor and a number to Title
We get an error, values in Go are all classified into different types, for example integers can be used in math operations, strings can be capitalized
Go is statically typed, which means that it knows what the types of your values are even before your program runs.
One of the package which is allow us to find out the type of value is reflect package and TypeOf function
In Go, a variable is a piece of storage containing a value. you can give a variable a name by using a variable declaration. Var keyword followed by the desired name and type of values the variable will hold.
var quantity int
var length,width float64
var customerName string
quantity = 2
customerName = "Damon Cole"
// assigning multiple variables at once
length,width = 1.2, 2.4
If you know beforehand what a variable value will be, you can declare variables and assign them values on the same line
Assing new value to existing variabels, need to be values of the same type. Go static typing ensures you do not accidentally assing the wrong kind of value to a variable
Also you can omit the variable type from the declaration when you want to assign and declare at the same time
var quantity = 4
var length, width = 1.2, 2.4
fmt.Println(reflect.TypeOf(quantity))
If you declare a variable without assigning it a value, that variable will contain the zero value for the numeric types, the zero value is actually 0 The zero value for string variable is an empty string, and the zero value for bool variabels is false and for the other its nil
A short variable declaration. Instead of explicitly declaring the type of the variable and later assigning to it with =, you do both at once using :=.
quantity := 4
length, width := 1.2, 2.4
customerName := "Damon Colee"
Because short variable declarations are so convenient and concise, they are used more often that regular declarations.
Go has one simple set of rules that apply to the names of variables, functions, and types:
-
A name must begin with a letter, and can have any number of additional letters and numbers
-
If the name of variable, function, or type begins with a capital letter, it is considered exported
-
If a variable/function/type name begins with a lowercase letter, it is considered unexported
-
If a name consists of multiple words, each word after the first shoulb be capitalized like this: topPrice, RetryConnection this style called camel case
-
When the meaning of a name is obvious from the context, use abbreviate it like this: use i instead of index, max instead of maximum and ...
Math and comparison operations in Go require that the include values be of the same type But what if you want to compare int to float or convert a string to integer There is solution for that and use conversions . You just provide the type want to convert a vallue to
var myInt int = 2
var floatValue := float64(myInt)
When making a conversion, be aware of how they might change the resulting For example, float64 variables can store fractional value but when you convert them to integers fractional portion is simply dropped
- Visit http://golang.org in your web browser
- Select the installation package
- Visit the installation instruction page for your OS
- Open a new terminal or command prompt window
- Confirm Go wa installed by typing go version at the prompt and that return go version for you if install correctly
- Using your favorite editor, save your first code
- Open a new terminal or command prompt
- In the terminal change dir to where file saved
- Run go fmt nameFile.go to clean up the code formatting
- Run go build namFile.go to compile the source code. This will add executable file to currect dir
- Run the executable file. On linux and macOs with this line ./nameFile and on windows namFile.exe
Command Description go build ==> Compiles source code files into binary files. go run ==> Compiles and runs a program, without saving an executable file. go fmt ==> Reformats source files using Go standard formatting. go version ==> Display the current Go version
Methods: fucntions that are asscociated with values of a given type, the methods that may have seen attached to object in other languages, but they are a bit simpler
The time package has a Time type that represents a date(year, month, day) Each time.Time value has a Year method that returns the year
The time.Now function returns a new Time value for the current date and time
Methods are functions that are associated with values of a partucular type.
string.NewPlacer()
replacer.Replace()
In most programming languages, fucntions and methods can only have a single return value, butn in Go they can return any number of values. in most cases function return an additional error value
When we have a value that would normally be assigned to a variable, but that we do not intend to use, we can use Go black identifier, simply type a single underscore (_) character in an assignment
We can set condition on error and track when error happend and we can use log package which is has a Fatal fucntion that can do both of these Operations for us at once: log a message to the terminal and stop the program : means reporting an error that kills your program
There is lot about error handling we talk about that later
Functions and methods like ReadString return an error value of nil, which basically means there is nothing there in other words if err is nil, it means there was no error
An expression is evaluated, and if its result is true, the code in the conditional block body is executed. If its false, the conditional block is skipped
Conditional rely on a boolean expression
Naming a variable error would be a bas idea, beacuse it would shadow the name of a type called error you should make sure it dosent have the same name as any existing fucntions,packages,types or other variables they can be create some issues later in your program
Declaring a variable like status without using it afterward is an error in Go. It seems a little strange that we re getting the error twice, but let disregard that for now Go code can be decided up into blocks, blocks are usually surrounded by curly braces {} The bodies of functions and conditinals are both blocks as well
Eeach variable you declare has a scope: a portion of your code that it visible within. if you inside of scope and try to access variables from outside of scope you will get an error but oposite its can be you have access to variables from outside of scope to inside of scope
Its true that when the same variable is declared twice in tha same scope we get a compile error
But as long as at least one variable name in short variable declarations is new, its allowed The new variable names are treated as a declaration, and the existing names are treated as an assignment
The math/rand package has a Intn fucntion that can generate a random number for us, so we will need to import math/rand. Then we will call rand. Intn to generate the random number
math/rand refering to the package import path not its name an import path is just a unique string that identifies a package and that you use in an import statement
the Go lang dosent require that a package name have anything to do with import path. but by convetion, the last or only segement of the import path is also used as the package name
so thats why out import statement uses a path of math/rand, but our main function just uses the package name rand
you have probably encountered loops. When you need one or more statements executed over and over, you place them inside a loop Loops always begin with the for keyword. for is followed by three segments of code that control the loop:
-
An initialization (or init) statement that is usually used to initialize a variable
-
A condition expression that deremines when to break out of the loop
-
A post statement that runs after each iteration of the loop
this loop print out 1 to 10
for i:=0 ; i<10 ; i++{
fmt.Println(i)
}
if you want, you can leave out the init and post statements from a loop, leaving only the conditon expression
x := 1
for x <= 3 {
fmt.Println(x)
}
also good to know the scope of any variable declared whithin a loops block is limited to that block
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"stirings"
"time"
)
func main() {
seconds := time.Now().Unix()
rand.Seed(seconds)
target := rand.Intn(100)+1
fmt.Println("I have chosen a random number between 1 and 100")
fmt.Println("Can you guess it?")
reader := bufio.NewReader(os.Stdin)
success := false
for guesses := 0 ; guesses < 10 ; guesses++ {
fmt.Println("You have 10-guesses")
fmt.Print("Make a guess ? ")
input, err := reader.ReadString("\n")
if err != nil{
log.Fatal(err)
}
input = strings.TrimSpace(input)
guess, err := strconv.Atoi(input)
if err != nil{
log.Fatal(err)
}
if guess < target {
fmt.Println("Oops your guess was low")
} else if guess > target{
fmt.Println("Oops your guess was high")
}else {
success = true
fmt.Println("GOOD JOB!")
break
}
}
if !success {
fmt.Println("Sorry, you lost")
}
}
The Sprintf function work just like printf, except that it returns a formatted string instead of print it
- Formatting verbs (the %0.2 in the strings above is a verb)
- Value width (that the 0.2 in the middle of the verb)
verb output
%f Floating-point number
%s String
%t Boolean (true or false)
%v Any value (choosen an approprate format based the supplied value type)
%#v Any value, formatted as it would appear in Go program code
%T Type of the supplied value (int string etc)
A simple function declaration might look like this
func sayHi() {
fmt.Println("Hi")
}
A declaration begins with the func keyword followed by the name want the function to have
The rules for variable names:
-
A name must begin with a capital letter are expoted
-
Names with multiple words should use camelcase
As with conditional and loop blocks, within function block are only in scope within that function blocks. variable at the package level access it within any function that package
If you need to format numbers or other values for use in your error message, you can use the fmt.Errorf() or you can use built in error package in Go which is take message and return error, error.New()
To declare multiple return values for a function, place the return types in a second set of paranthese in the function declaration
You can also specify a name for return type of value it make the purpose of the return values clearer, The main purpose of named return values is as documentation for programming reading the code.
When you call a function that has parameters declared, you need to provide arguments to the call. The value in each argument is copied to the corresponding parameter variable. pass by value
Go is a pass-by-value language fucntion parameters receive a copy of the arguments from the function call
You can get the address of a variable using & which is Go s address of operator We can get address for variables of any type. Notice that the address diffres for each variable
Values that represent the address of a variable are known as pointers, because they point to the location where the variable can be found
The type of a pointer is written with * symbol for example *int you can read that aloud as ponter int
A pointer variable can only hold ponters to one type of value, so a variable might only hold *int ponters, only *float64 and so on
var myBool bool
myBoolPointer := & myBool
fmt.Println(myBoolPointer) // address of myBool value
Its possible to return pointer from function, just declare that the fucntion return type is a pointer type
func createPonter () *float64 {
var myFloat = 98.5
return &myFloat
}
func main(){
var myFloatPointer *float64 = createPonter()
fmt.Println(&myFloatPointer)
}
Its okey to return a ponter to a variable that local to a function. Even though that variable is no longer is scope, as long as you still have the pointer, Go will ensure you can still access the value