0% found this document useful (0 votes)
53 views

01 Golang Basics

The document discusses the basics of Golang including variables, basic types, constants, functions, flow control, loops and packages. It covers numeric types like integers and floats, and other types like booleans and strings.

Uploaded by

returnstrike
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

01 Golang Basics

The document discusses the basics of Golang including variables, basic types, constants, functions, flow control, loops and packages. It covers numeric types like integers and floats, and other types like booleans and strings.

Uploaded by

returnstrike
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

The Golang Basics

What we cover on this lecture?


• Variables
• Basic types
• Constants
• functions
• Flow-control
• Loops
• Packages
Golang keywords

You cannot use key words as variables, types, function names in your program
Predeclared names
Variables
Declare a variable in or out of the function
Declare a variable in or out of the function

var name type


(Omit the value, default will be used)

Example:
var x int
(value will be 0)
Declare a variable in or out of the function

var name = expression


(Omit the type, type will be computed)

Example:
var x = 5
(type will be int)
Declare a variable in or out of the function

You can group declaration into one block

Example:
var (
x1 int = 5
x2 = 8 // type will be the same as x1
)
Declare a variable in the function

name := expression
(short variable declaration, type will be computed)
Can be used only inside a function/method

Example:
x := 5
(type will int)
Naming conventions

• Begins with a letter (Unicode) or underscore (_);


• May have letters, digits, underscores;
• Case matters: “goLang” and “GoLang” are different names.

• Use CamelCase for word separation


• Long of the variable name should be connection with the scope of the variable
(less scope, less characters in name preferred)

Validname, valid_name, _validname, _v_a_l, valid_123

123, invalid-name, invalid!, 2e


Variables initialization
• func main() {
• var (
Boolean - false
• b1 bool String - ""
• s1 string Integer - 0
• i1 int
Unsigned Integer - 0
• ui1 uint
• by1 byte Byte - 0
• r1 rune Rune - 0
• f1 float32 Float number - 0
• c1 complex64
Complex number - (0+0i)
• )

fmt.Println("Boolean - ", b1)
• fmt.Printf("String - %q\n", s1)
• fmt.Println("Integer - ", i1) https://goplay.tools/snippet/LUmySkr
• fmt.Println("Unsigned Integer - ", ui1)
• fmt.Println("Byte - ", by1)
emfm
• fmt.Println("Rune - ", r1)
• fmt.Println("Float number - ", f1)
• fmt.Println("Complex number - ", c1)
• }
Every literal has it’s own default type
• package main
• String literal – string
import (
Integer literal – int
• "fmt"
Character literal - int32
• )
• Floating number literal - float64
func main() {
• s := ""

• c := 'b'
• i := 0
https://goplay.tools/snippet/xnqb_jHuyNr
• f := 0.0

fmt.Printf("String literal - %T\n", s)
• fmt.Printf("Integer literal - %T\n", i)
• fmt.Printf("Character literal - %T\n", c)
• fmt.Printf("Floating number literal - %T\n", f)
• }
Variables in Golang key points

• Every variable is initialized with a specified or default value.


• Each type has its default value.
• Every literal has its default type.
• You cannot compare numbers of different types, for example int and int8 is completely
different types from Golang prospective, you cannot compare or reassign variables with
different types.
Questions
Basic types
Basic types
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8

rune // alias for int32


// represents a Unicode code point

float32 float64
complex64 complex128
Boolean type
• package main true
• false
import (
• "fmt"
• ) Boolean is very simple type, in Golang it is just a
• constant which hold result of two expressions.
func main() {
• // var truthy = true const (
• var truthy bool = 14 > 12 true = 0 == 0 // Untyped bool.
• // var falsy = false false = 0 != 0 // Untyped bool.

• var falsy bool = 14 < 12 )


fmt.Println(truthy)
• fmt.Println(falsy)
• } https://goplay.tools/snippet/b6yluuxtOWk
Logical operators
Numeric types
Signed integer
• func main() { -1 3 5
64 32 64
• var (
• a int = -1
• aSize = unsafe.Sizeof(a) * 8 https://goplay.tools/snippet/JtZ2_7tT0i-

b int32 = 3 Machine dependent types:
• bSize = unsafe.Sizeof(b) * 8 int – depending on architecture, take 32/64 bit

• Machine un dependent types:


c int64 = 5 int8, int16, int32, int64 – take 8/16/32/64 bit
independently on architecture.
• cSize = unsafe.Sizeof(c) * 8
• )

fmt.Println(a, b, c)
• fmt.Println(aSize, bSize, cSize)
• }
Unsigned integer
• func main() { 1 3 5
• var ( 32 64 64
• a uint = 1
• aSize = https://goplay.tools/snippet/vgzxy6VNukc
unsafe.Sizeof(a) * 8

b uint32 = 3 Machine dependent types:
• bSize = • uint – depending on architecture, take 32/64 bit
unsafe.Sizeof(b) * 8

c uint64 = 5 Machine un dependent types:

• cSize = • uint8, uint16, uint32, uint64 – take 8/16/32/64 bit


unsafe.Sizeof(c) * 8 independently on architecture.

• )

fmt.Println(a, b, c)
• fmt.Println(bSize, aSize,
cSize)
• }
Compare integers
• func main() { ./prog.go:14:16: invalid operation: a == b
• var ( (mismatched types int and int32)

• a int
./prog.go:15:16: invalid operation: a == c
• b int32
(mismatched types int and int64)
• c int64
• ) https://goplay.tools/snippet/ioHcc7Jzy-H


fmt.Println(a == b)
• fmt.Println(b == c) You cannot compare different types of integers,
for example int and int32.
• }
Compare integers
• func main() { true
• var ( true
• a int
• b int32 https://goplay.tools/snippet/CypY4iH

• c int64 ozG8

• )

fmt.Println(a == int(b) To compare two different types, we
)
can use type casting.
• fmt.Println(int64(b) ==
c)
• }
Integer binary operators
Increment/Decrement
• func main() { 1
• var a int
0
• a++
-128
• fmt.Println(a)
• -32768
a--
-9223372036854775808
• fmt.Println(a)
• https://goplay.tools/snippet/NBDiiSUtzii
b := int8(math.MaxInt8)
• b++
• fmt.Println(b)
Increment and decrement operation in Golang exists

c := int16(math.MaxInt16) only in a suffix-based form, you can write only “i++” not
• c++ “++i”, as well increment operation is mutate value and
• fmt.Println(c)
doesn’t return value of the operation, due to this “j :=

d := int64(math.MaxInt64) i++” is invalid operation, as I mentioned before Golang is
• d++ explicit language, you need to write exactly what to
• fmt.Println(d) expect, due to this some common constructions is
• }
forbidden.
Floating-point numbers
Floating point numbers
• func main() { 1
• var f64 float64 1
true
• f64++
• fmt.Println(f64)
• https://goplay.tools/snippet/c07pmPGQMkW
var f32 float32
• f32++ In Golang there is no double and float type,
• fmt.Println(f32) there is only float32 and float64

//invalid operation: f32 == f
64 (mismatched types float32 and Float32 occupies 32 bits in memory and stores
float64) values in single-precision floating point
• //fmt.Println(f32 == f64) format.

fmt.Println(float64(f32) == f
64) Float64 occupies 64 bits in memory and stores
• } values in double-precision floating point
format.
String type
Strings
• package main Dzień dobry

import (
https://goplay.tools/snippet/7yGc4jBf7uQ
• "fmt"
• )
• Why len(msg) returns 12 when we have 11
func main() {
characters? There answer is quite simple,
• msg := "Dzień dobry"
len returns value in bytes, and character
• //msg is a string with
11 characters “ń” cost us 2 bytes in UTF-8 encoding (non-
• ASCI character).
fmt.Println(len(msg)) /
/12
• // Why we have 12 here
?
• }
Converting the string
• package main Dzień dobry

import ( https://goplay.tools/snippet/7yGc4jBf7uQ
• "fmt"
• )

func main() {
Why len(msg) returns 12 when we have 11
• msg := "Dzień dobry"
characters? There answer is quite simple,
• //msg is a string with
11 characters len returns value in bytes, and character
• “ń” cost us 2 bytes in UTF-8 encoding (non-
fmt.Println(len(msg)) /
/12 ASCI character).

• // Why we have 12 here


?
• }
Converting the string

We will speak more about strings on the next lection :)


And about types on next and next after the next :-)
Default values
Questions
Constants
Constant declarations

const name type = expression


(Full syntax, strict type)

Example:
const x int = 34
Constant declarations

const name = expression


(Omit the type, type will be computed from expression)

Example:
const x = 5
(type will be untyped integer)
(untyped integer)
Naming conventions
• Begins with a letter (Unicode) or underscore (_);
• May have letters, digits, underscores;
• Case matters: “goLang” and “GoLang” are different names.

• Use CamelCase for word separation

Validname, valid_name, _validname, _v_a_l, valid_123

123, invalid-name, invalid!, 2e


Constants in Golang key points
• Every constant must have initialization expression, there no default value for constants.

• Every constant vithout specified strict type has more weak rules in terms of types system, you
can compare number constant with another constant or variable of any numbered type or aliased
type created from numbered, the same for strings.

• Every constant can have strict type, in this case you cannot compare, assign them to variables of
other type.
Constants
• const name string = "Go" Go
• const ( Java
• e = 2.7182 1.1 1.1 2 2
• pi = 3.1415 float64 int
• ) https://goplay.tools/snippet/O-po3ANlo6u
• const (
• a = 1.1 Constants in Golang are variables that has in
• b place initialization and that value cannot be
• c = 2 changed after first initialization. (One
• d exception, you can in smaller scope define
variable with the same name)
• ) Constants have a little bit different type
• system, strict type of the constant computed when
func main() {
it used, it means that you can compare constant e
• fmt.Println(name)
with float32 and float64 type, as well as
• name := "Java" constant b can be used without type casting with
• fmt.Println(name) any numbered type. The same applicable with type
• aliases(we will talk about it later.)
fmt.Println(a, b, c, d)
• fmt.Printf("%T %T\n", b, d
)
• }
Iota and constants
• type Weekday int
0 1 2

const (
0 4 6
• Monday Weekday = iota
• Tuesday = iota
• Wednesday = iota https://goplay.tools/snippet/6DqDZZH1SCi
• )

const (
• a = iota * 2 Iota is a constant with special behavior,
• _ it is kind of generator that return unique
• b
sequential number for constant block.
• c
• ) • Value of iota not shared across different
• constants blocks, but
func main() {
• fmt.Println(Monday, Tuesda You can change value of iota using
y, Wednesday) •

• fmt.Println(a, b, c) standard operations as +, *, -.


• }
• In case you want to skip value you can
use blank identifier _
Type system variables vs constants
VA R I A B L E S C O N S TA N T S
• Variables has strict type system, • Constant has weak type system, for
int8 and int32 for example is example string constant has type
different type and can’t be used untyped string constant and numbers
interchangeably. untyped integer, final type computed
when constant is used, this allows to
use constants with any castable type.
Questions
Functions
Function declarations

func name(parameters) (results) {


//body
}
Function types
• package main func(int, int) int
• func(int, int) int
import "fmt"
• func(int, int) int
func add(x int, y int) int func(int, int) int
{ return x + y }
• func sub(x, y int) (z int)
{ z = x - y; return } https://goplay.tools/snippet/cpbj8m-40rw
• func first(x int, _ int) int
{ return x }
• func zero(int, int) int Function in Golang has their own
{ return 0 }
• type.
func main() {
Function name must be unique in the
• fmt.Printf("%T\n", add)
scope, you cannot override function.
• fmt.Printf("%T\n", sub)
• fmt.Printf("%T\n", first)
• fmt.Printf("%T\n", zero)
• }
Recursion
• package main
120

import "fmt"

func factorial(i uint) uint { https://goplay.tools/snippet/Re_kjE0PHAl
• if i == 0 {
• return 1
• }
• return i * factorial(i-1)
• }

func main() {
• fmt.Println(factorial(5))
• }
Multiple return values
• package main 3 1

https://goplay.tools/snippet/fTlb7HpuDyi
import (
• "fmt"
• ) Functions in Golang can have multiple
• return parameters, it gives an
func main() {
ability to have pretty elegant
• fmt.Println(devide(10, 3))
solutions, but be conservative with
• }
this feature, large number or return

func devide(a, b int) (int, int) statement usually signal that you
{
break single responsibility
• return a / b, a % b
principle.
• }
Variadic parameter
• func main() {
10
• digits := []int{1, 2, 3, 4}

//You cannot just pass an array, you h
ave to expand it https://goplay.tools/snippet/Wobv8_U_y9d
• //fmt.Println(sum(digits)) // Will not
work
• fmt.Println(sum(digits...)) You need to keep in mind a few things:
• } • Variadic parameter is a slice under the
• hood (slice is a dynamic array in Golan,
//variadic parameter must be last paramete
r of the function about this type later)
• func sum(digits ...int) int { • Variadic parameter must be last in a
• var sum int function
• for _, d := range digits {
• You need to expand the slice to pass it as
• sum += d
a variadic parameter with ”...”
• }

return sum
• }
Change underlined slice in variadic parameter
• func main() {
• strs := []string{
Hello, My name is Michal
• "Hello,", "My", "name",
• "is", "Alex",
https://goplay.tools/snippet/6imaJjAAB0A
• }
• mutate(strs...)
• You need to keep in mind a few
fmt.Println(strings.Join(str
s, " ")) things:

• } • ”...” doesn’t copy slice due to this

• you can change slice inside a variadic


func mutate(x ...string) { function
• x[len(x)-1] = "Michał"
• }
High-order functions
• package main
Start execution

import "fmt" Hello world!!!

func logExecution(f func()) { Executed
• fmt.Println("Start execution
")
• f() https://goplay.tools/snippet/ue1Ikd67GCp
• fmt.Println("Executed")
• }
What we just did?

func main() { • Assign a function to a variable

hello := func() { fmt.Printl • Pass a function to the function as
n("Hello world!!!") }
• a parameter
logExecution(hello)
• }
High-order functions
• package main Hello Poland!

import "fmt" Cześć!

func newWriter() func(string) {
https://goplay.tools/snippet/4y54T8g5qst
• return func(s string) {
• fmt.Println(s)
What we just did?
• }
• } • Assign a function to a variable
• func main() { • Return a function from the
• var writer func(string) = ne function
wWriter()
• Define anonymous function
• writer("Hello Poland!")
(function without name)
• writer("Cześć!")
• }
Deferred functions
• package main Exit from function

import ( I will be executed after exit from
• "fmt" the function
• )

func main() {
https://goplay.tools/snippet/AM29trKSYZK
• var message string = "I w
ill be executed after exit fr
om the function"

defer func() {
• fmt.Println(message)
• }()

fmt.Println("Exit from fu
nction")
• }
Deferred functions execution order

• package main 4
• 3
import "fmt" 2
• 1
func main() {
0
• for i := 0; i < 5; i++ {
• defer fmt.Println(i)
• }

// deferred funcs run here
• }

https://goplay.tools/snippet/ePn-bHg2S-X
Deferred functions
• package main 10 10 10 10 10 10 10 10 10 10

import (
• "fmt" https://goplay.tools/snippet/URnuA-CzSE_V
•)

func main() {
• for i := 0; i < 10;
i++ {
• defer func() {
• fmt.Print(i,
" ")
• }()
• }
•}
Deferred functions use cases

row, err := db.Query(`SELECT ...`) defer func() {

if err != nil { if err := recover(); err != nil {


// handle error or path it to the ...
caller }
} }()
defer row.Close()
panic("oops!")

https://goplay.tools/snippet/dEShiFiGuCO
Function in Golang key points
• Function is just a bunch of operationgs grouped in a logical way.

• You can assign function to variable, pass it as a parameter to the function or


return from the function.

• Functions in Golang has type as well, type of the function depending on its
parameters and return statements.
Questions
Flow control
If – else statement
if condition { If else syntax is common for most
languages, the only difference is
... that you don’t need parentheses
around conditions, but the braces
} else if condition{ are required.

...

} else {

...
}
If - else
• func main() {
unexpected error - invalid value, valid
• for i := 0; i <= 7; i++ {
• if weekday, err := isWeekDay(i); er range is [1-7]
r != nil {
1 – true
• fmt.Println("unexpected error -
", err) 2 – true
• } else {
• fmt.Println(i, "-", weekday) 3 – true
• } 4 – true
• } 5 – true
• }
6 – false

func isWeekDay(d int) (bool, error) { 7 - false
• if d <= 0 || d > 7 {
• return false, fmt.Errorf("invalid v
alue, valid range is [1-7]")
• } else if d > 5 { https://goplay.tools/snippet/V27mHXgTT42
• return false, nil
• } else {
• return true, nil
• }
• }
Switch statement
• func main() {
• fmt.Println(numberToWeekDay(1)) Monday
• fmt.Println(numberToWeekDay(3)) Wednesday
• }
• func numberToWeekDay(i int) string {
• switch i {
• case 1: https://goplay.tools/snippet/YMHG82I9nFN
• return "Monday"
• case 2:
• return "Tuesday" Switch statement in Golang has pretty
• case 3:
• return "Wednesday" common syntax, one important difference
• case 4:
• return "Thursday" is that instead of falltrough behavior,
• case 5: Golang breaks after each case.
• return "Friday"
• case 6:
• return "Saturday"
• case 7:
• return "Sunday"

default:
• return "unknown"
• }
• }
Switch statement
• func main() {
• fmt.Println(isWeekDay(1)) true <nil>
• false <nil>
fmt.Println(isWeekDay(6))

fmt.Println(isWeekDay(10)) false invalid value, valid range [1-7]
• }

func isWeekDay(i int) (bool, error) {
https://goplay.tools/snippet/SMVOi9WLJag
• switch i {
• case 1, 2, 3, 4, 5:
• return true, nil
• case 6, 7: In case you have the same behaviour for
• return false, nil multiple values, you can specify multiple
• default:
values in one case.
• return false, fmt.Errorf("invalid v
alue, valid range [1-7]")
• }
• }
Switch statement
• func main() {
true <nil>
• fmt.Println(isWeekend(6))
• false <nil>
fmt.Println(isWeekend(5))
• false invalid value, valid range [1-7]
fmt.Println(isWeekend(10))
• }

func isWeekend(i int) (bool, error) { https://goplay.tools/snippet/xENCYSAAXeu

switch {
• case i >= 6 && i <= 7:
• return true, nil You can implement all use-cases of if-
• case i >= 1 && i <= 5:
else statement using switch statement, in
• return false, nil
• default: case of absence of variable in switch you
• return false, fmt.Errorf("invalid v can just specify expression with boolean
alue, valid range [1-7]")
• } result.
• }
Questions
Loops
Loops (Plain old for loop)
• package main 0 1 2 3 4 5 6 7 8 9


import ( https://goplay.tools/snippet/KEBAFKZvwvx

• "fmt"
Plain for loop syntax is common for most
• ) languages, the only difference is that
• you don’t need parentheses around
func main() { conditions, but the braces are required.
• for i := 0; i < 10; i++ {
• fmt.Print(i, " ")
• }
• }
Loops (While loop)
• package main 0 1 2 3 4 5 6 7 8 9

import ( https://goplay.tools/snippet/Fbp11kKVQw4

• "fmt"
In Golang while loop is special type of
• )
for loop.

func main() {
• var i int
• for i < 10 {
• fmt.Print(i, " ")
• i++
• }
• }
Loops (Infinite loop)
• package main Hello from infinite loop
• Hello from infinite loop
import (
Hello from infinite loop
• "fmt"
Hello from infinite loop
• )
Hello from infinite loop

func main() { ...

for {
https://goplay.tools/snippet/AhPisV44upd
• fmt.Println("Hello from i
nfinite loop")
• } Empty values works as infinite loop
• }
Loops (For each)
• func main() { Hello from for-each loop !
• strs := []string{
• "Hello", https://goplay.tools/snippet/JvDY6ZHkrUY
• "from",
• "for-each", For range loop is helpful do go over
• "loop", collections, range key word used, first
• "!", parameter returned by range is index of

• } the element, second parameter is actual


value.

for _, s := range strs {
• fmt.Print(s, " ")
• }
• }
Loops (continue, break)
• func main() {
Hello from loop
• strs := []string{
• "Hello",
• "from", https://goplay.tools/snippet/u6f1sHcxdbM
• "for-each",
• "loop", You can use break and continue operators,
• "!", to control loop behavior.
• }

for _, s := range strs { Continue stop current iteration and start
• if s == "for-each" {
next one.
• continue
• } else if s == "!" {
• break Break completely stop loop.
• }
• fmt.Print(s, " ")
• }
• }
What should you remember?
• In Golang there is only for loop, no while or do-while.

• Loop variable initialized only once; you need to copy it before using in
closures or path to a function in case it reference type.

• For loop contains three parts, variable initialization, condition, post


statement any of this part can be skipped or extended with different
conditions.
Packages
Golang packages
~/go/src/github.com/burov:

greeting/
├── go.mod
├── main.go
└── language
├── polish
│ └── polish.go
├── english
│ └── english.go
└── language.go
Golang packages
github.com/burov/greeting/language/polish

package polish
~/go/src/github.com/burov: const Name = ”Polish"

greeting/
├── go.mod
├── main.go github.com/burov/greeting/language/english
└── language
├── polish package english
│ └── polish.go
├── english const Name = "English"
│ └── english.go
└── language.go

github.com/burov/greeting/language

package language

const Name = "Language"


Golang packages
package main

import (
"fmt" github.com/burov/greeting/language/deutsch

"github.com/burov/greeting/language"
package deutsch
"github.com/burov/greeting/language/english"
~/go/src/github.com/burov: ) const Name = "Deutsch"

greeting/ func main() {


├── go.mod fmt.Println(language.Name)
├── main.go fmt.Println(english.Name) github.com/burov/greeting/language/english
└── language }
├── polish package english
│ └── polish.go
├── english const Name = ”Polish"
│ └── english.go
└── language.go

github.com/burov/greeting/language

package language

const Name = "Language"


Exported/unexported objects

~/go/src/github.com/burov:

greeting/
├── go.mod
├── main.go
└── language github.com/burov/greeting/language/english
├── polish
│ └── polish.go package english
├── english
│ └── english.go const Name = "English"
const internalName = "eng"
└── language.go
Exported/unexported objects
package main

import (
"fmt"

"github.com/burov/greeting/language/english"
)
~/go/src/github.com/burov:
func main() {
greeting/ fmt.Println(english.Name)
├── go.mod fmt.Println(english.internalName)
├── main.go } github.com/burov/greeting/language/english
└── language
├── polish package english
│ └── polish.go
├── english const Name = "English"
const internalName = "en"
│ └── english.go
└── language.go
Exported/unexported objects
package main

import (
"fmt"

"github.com/burov/greeting/language/polish"
)
~/go/src/github.com/burov:

greeting/ func main() {


fmt.Println(polish.Name)
├── go.mod fmt.Println(polish.internalName)
├── main.go } github.com/burov/greeting/language/english
└── language
├── polish package english
│ └── polish.go
├── english const Name = ”Polish"
const internalName = ”pl"
│ └── english.go
└── language.go

./main.go:10:14: cannot refer to unexported name polish.internalName


./main.go:10:14: undefined: polish.internalName
Packages example
• package main English

import ( Polish
https://goplay.tools/snippet/5TtAEQg-ZN9
• "fmt"

"github.com/burov/greeting/langua To import package, you need to use module
ge/english" name + package or if you use $GOPATH just a
• "github.com/burov/greeting/langua relative path from $GOPATH/src + package
ge/polish"
• )
Every variable/function/constant/structure named
• from upper-case letter is exported (you can
func main() {
use them from other packages), from lower-
• fmt.Println(english.Name)
case letter is package private only.
• fmt.Println(polish.Name)
• }
Packages example
• package main
English

import (
Polish
• "fmt"

"github.com/burov/greeting/langua
ge/english" https://goplay.tools/snippet/GSHnaLmADtH
• "github.com/burov/greeting/langua
ge/polish"
• ) To import package, you need to use module
• name + package or if you use $GOPATH just a
func main() {
relative path from $GOPATH/src + package
• fmt.Println(english.Name)
• fmt.Println(polish.Name)
• Every variable/function/constant/structure named
//fmt.Println(english.internalNam
e) from upper-case letter is exported (you can
• //cannot refer to unexported name use them from other packages), from lower-
english.internalName
• case letter is package private only.
//fmt.Println(polish.internalName
)
• //cannot refer to unexported name
polish.internalName
• }
Summary about packages
• Package is just a folder with files, in one folder you can have only
one package.

• Every file start from package definition, as a good practice name of


the package is equals to folder name.

• Main package is a special package that is entry point of the code

• Every variable/function/constant/structure named from upper-case letter is


exported and can be accessed outside of the project.

• Every variable/function/constant/structure named from lower-case letter is


package private and cannot be accessed outside of the project.
Questions
Homework
Homework “Square task”

How to: Tasks:


•Clone the repo Implement function to calculate square of an equilateral figurine
•run go mod init somename following rules:
•run go mod tidy •func CalcSquare(sideLen float64, sidesNum intCustomType) float64
•Edit solution.go •CalcSquare func must return correct square for:
•it contains correct package name •equilateral triangle(3 sides),
•follow comments placeholder •square(4 sides)
•circle(0 sides) (count sideLen as radius)
•if any other sideNum param is passed, return 0
•built-in Pi constant must be used to bypass the test
Thanks

You might also like