+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 : Variables
compiler user type inference
13 Demo : Variables
Package Main -- specifies that we have a main package
And it allows us to have the func main () : Main function is the first function that is run when you run a go program .
For most of the demo we will be utilizing the main package , along with the main function to implement and test out our different code.
You will see that import the fmt package , fmt is the format package that is part of the standard library and it is used to print out information . So we will print out information and creating variable in this program.
The first thing that we do is create a string variable .
fmt.println -- that is short for print line.
to save it press control +s
to run it - Open a terminal
package main
import "fmt"
func main() {
var myName = "Jayson"
fmt.Println("My Name is", myName)
}
package main
import "fmt"
func main() {
var myName = "Jayson"
fmt.Println("My Name is", myName)
var name string = "Kathy"
fmt.Println("Her name is :", name)
}
import "fmt"
func main() {
var myName = "Jayson"
fmt.Println("My Name is", myName)
var name string = "Kathy"
fmt.Println("Her name is :", name)
userName := "Admin"
fmt.Println("userName : ", userName)
}
package main
import "fmt"
func main() {
var myName = "Jayson"
fmt.Println("My Name is", myName)
var name string = "Kathy"
fmt.Println("Her name is :", name)
userName := "Bullseye"
fmt.Println("userName : ", userName)
var sum int
fmt.Println("Sum is :", sum)
}
package main
import "fmt"
func main() {
var myName = "Jayson"
fmt.Println("My Name is", myName)
var name string = "Kathy"
fmt.Println("Her name is :", name)
userName := "Bullseye"
fmt.Println("userName : ", userName)
var sum int
fmt.Println("Sum is :", sum)
part1, other := 1, 5
fmt.Println("Part1 is :", part1, "other is :", other)
}
package main
import "fmt"
func main() {
var myName = "Jayson"
fmt.Println("My Name is", myName)
var name string = "Kathy"
fmt.Println("Her name is :", name)
userName := "Bullseye"
fmt.Println("userName : ", userName)
var sum int
fmt.Println("Sum is :", sum)
part1, other := 1, 5
fmt.Println("Part1 is :", part1, "other is :", other)
part2, other := 2, 3
fmt.Println("Part2 is : ", part2, "Other is :", other)
}
package main
import "fmt"
func main() {
var myName = "Jayson"
fmt.Println("My Name is", myName)
var name string = "Kathy"
fmt.Println("Her name is :", name)
userName := "Bullseye"
fmt.Println("userName : ", userName)
var sum int
fmt.Println("Sum is :", sum)
part1, other := 1, 5
fmt.Println("Part1 is :", part1, "other is :", other)
part2, other := 2, 3
fmt.Println("Part2 is : ", part2, "Other is :", other)
sum = part1 + part2
fmt.Println("sum value is :", sum)
}
package main
import "fmt"
func main() {
var myName = "Jayson"
fmt.Println("My Name is", myName)
var name string = "Kathy"
fmt.Println("Her name is :", name)
userName := "Bullseye"
fmt.Println("userName : ", userName)
var sum int
fmt.Println("Sum is :", sum)
part1, other := 1, 5
fmt.Println("Part1 is :", part1, "other is :", other)
part2, other := 2, 3
fmt.Println("Part2 is : ", part2, "Other is :", other)
sum = part1 + part2
fmt.Println("sum value is :", sum)
var (
lessonName = "Variables"
lessonType = "Demo"
)
fmt.Println("lessonName is :", lessonName)
fmt.Println("lessonType is :", lessonType)
word1, word2, _ := "hello", "world", "!"
fmt.Println("word is :", word1, "word2 is :", word2)
}
14. Exercise :
//Summary:
// Print basic information to the terminal using various variable
// creation techniques. The information may be printed using any
// formatting you like.
//
//Requirements:
//* Store your favorite color in a variable using the `var` keyword
//* Store your birth year and age (in years) in two variables using
// compound assignment
//* Store your first & last initials in two variables using block assignment
//* Declare (but don't assign!) a variable for your age (in days),
// then assign it on the next line by multiplying 365 with the age
// variable created earlier
//
//Notes:
//* Use fmt.Println() to print out information
//* Basic math operations are:
// Subtraction -
// Addition +
// Multiplication *
// Division /
package main
import "fmt"
func main() {
//Requirements:
//* Store your favorite color in a variable using the `var` keyword
var favoriteColor = "Yellow"
fmt.Println("My favourite color is :", favoriteColor)
//* Store your birth year and age (in years) in two variables using
birthYear, ageInYears := 1981, 41
fmt.Println("Born in :", birthYear)
fmt.Println("My age is :", ageInYears, "Years")
// compound assignment
//* Store your first & last initials in two variables using block assignment
var (
firstInitial = "S"
lastInitial = "B"
)
fmt.Println("My First Initial is :", firstInitial, "My Last initial is :", lastInitial)
//* Declare (but don't assign!) a variable for your age (in days),
// then assign it on the next line by multiplying 365 with the age
// variable created earlier
//
var ageInDays int
ageInDays = 365 * ageInYears
fmt.Println("I am", ageInDays, "days old")
}
15. Basic Functions :
You can have a function that takes no input and displays no output, the example for this can be a generic Message
package main
import "fmt"
func double(x int) int {
return x + x
}
func add(lhs, rhs int) int {
return lhs + rhs
}
func greet() {
fmt.Println("Hello from greet function !")
}
func main() {
dozen := double(3)
fmt.Println("This is double the number :", dozen)
greet()
bakersDozen := add(dozen, 1)
fmt.Println("This is the baker's dozen :", bakersDozen)
anotherBakersDozen := add(double(6), 1)
fmt.Print("Another Baker's dozen : ", anotherBakersDozen)
}
//--Summary:
// Use functions to perform basic operations and print some
// information to the terminal.
//
//--Requirements:
//* Write a function that accepts a person's name as a function
// parameter and displays a greeting to that person.
//* Write a function that returns any message, and call it from within
// fmt.Println()
//* Write a function to add 3 numbers together, supplied as arguments, and
// return the answer
//* Write a function that returns any number
//* Write a function that returns any two numbers
//* Add three numbers together using any combination of the existing functions.
// * Print the result
//* Call every function at least once
package main
import "fmt"
//* Write a function that returns any two numbers
func twoTwos() (int, int) {
return 2, 2
}
//* Write a function that returns any number
func five() int {
return 5
}
//* Write a function to add 3 numbers together, supplied as arguments, and
// return the answer
func addThree(a, b, c int) int {
return a + b + c
}
//* Write a function to add 3 numbers together, supplied as arguments, and
// return the answer
func greetPerson(name string) {
fmt.Print("Hello !", name)
}
//* Write a function that returns any message, and call it from within
// fmt.Println()
func hiThere() string {
return " Hi There !"
}
func main() {
greetPerson("Alisa")
fmt.Println(hiThere())
a, b := twoTwos()
answer := addThree(five(), a, b)
fmt.Println(answer)
}
18 ---
19 .
19. Demo If else
package main
import "fmt"
func average(a, b, c int) float32 {
// Convert the sum of the scores into a float32
return float32(a+b+c) / 3
}
func main() {
quiz1, quiz2, quiz3 := 9, 7, 8
if quiz1 > quiz2 {
fmt.Print("quiz1 scored greater than quiz2")
} else if quiz1 < quiz2 {
fmt.Println("quiz2 scored higher than quiz1")
} else {
fmt.Println("quiz1 & quiz2 have scored the same ")
}
}
if average(quiz1, quiz2, quiz3) > 7 {
fmt.Println("acceptable grades")
} else {
fmt.Println("Not an acceptable range")
}
20 .Exercise If-Else
//--Summary:
// The existing program is used to restrict access to a resource
// based on day of the week and user role. Currently, the program
// allows any user to access the resource. Implement the functionality
// needed to grant resource access using any combination of `if`, `else if`,
// and `else`.
//
//--Requirements:
//* Use the accessGranted() and accessDenied() functions to display
// informational messages
//* Access at any time: Admin, Manager
//* Access weekends: Contractor
//* Access weekdays: Member
//* Access Mondays, Wednesdays, and Fridays: Guest
package main
import (
"fmt"
)
// Days of the week
const (
Monday = 0
Tuesday = 1
Wednesday = 2
Thursday = 3
Friday = 4
Saturday = 5
Sunday = 6
)
// User roles
const (
Admin = 10
Manager = 20
Contractor = 30
Member = 40
Guest = 50
)
func weekday(day int) bool {
return day <= 4
}
func accessGranted() {
fmt.Println("Access Granted")
}
func accessDenied() {
fmt.Println("Access Denied")
}
func main() {
// The day and role. Change these to check your work.
today, role := Tuesday, Guest
//* Use the accessGranted() and accessDenied() functions to display
// informational messages
//* Access at any time: Admin, Manager
if role == Admin || role == Manager {
accessGranted()
} else if role == Contractor && !weekday(today) {
accessGranted()
} else if role == Member && weekday(today) {
accessGranted()
} else if role == Guest && (today == Monday || today == Wednesday || today == Friday) {
accessGranted()
} else {
accessDenied()
}
//* Access weekends: Contractor
//* Access weekdays: Member
//* Access Mondays, Wednesdays, and Fridays: Guest
//* accessGranted()
}
package main
import "fmt"
func price() int {
return 3
}
const (
Economy = 0
Business = 1
FirstClass = 2
)
func main() {
switch p := price(); {
case p < 2:
fmt.Println("Cheap Price")
case p < 10:
fmt.Println("Moderately Priced")
default:
fmt.Println("Expensive Item")
}
ticket := Economy
switch ticket {
case Economy:
fmt.Println("Economy Seating")
case Business:
fmt.Println("Business Seating")
case FirstClass:
fmt.Println("First Class Seating")
}
}
23 . Excercise Switch :
//--Summary:
// Create a program to display a classification based on age.
//
//--Requirements:
//* Use a `switch` statement to print the following:
// - "newborn" when age is 0
// - "toddler" when age is 1, 2, or 3
// - "child" when age is 4 through 12
// - "teenager" when age is 13 through 17
// - "adult" when age is 18+
package main
import "fmt"
func main() {
switch age := 3; {
case age == 0:
fmt.Println("newBorn")
case age >= 1 && age <= 3:
fmt.Println("Toddler")
case age < 13:
fmt.Println("Child")
case age < 18:
fmt.Println("Teenager")
default:
fmt.Println("Adult")
}
}
24 . Looping :
25 . Demo : Looping :
25. Demo Lopping :
package main
import "fmt"
func main() {
sum := 0
fmt.Println("Sum is :", sum)
for i := 1; i <= 10; i++ {
sum += i
fmt.Println("Sum is :", sum)
}
}
26. Exercise Loops :
//--Summary:
// Implement the classic "FizzBuzz" problem using a `for` loop.
//
//--Requirements:
//* Print integers 1 to 50, except:
// - Print "Fizz" if the integer is divisible by 3
// - Print "Buzz" if the integer is divisible by 5
// - Print "FizzBuzz" if the integer is divisible by both 3 and 5
//
//--Notes:
//* The remainder operator (%) can be used to determine divisibility
package main
import "fmt"
func main() {
for i := 1; i <= 50; i++ {
divisibleBy3 := i%3 == 0
divisibleBy5 := i%5 == 0
if divisibleBy3 && divisibleBy5 {
fmt.Println("FizzBuzz")
} else if divisibleBy3 {
fmt.Println("Fizz")
} else if divisibleBy5 {
fmt.Println("Buzz")
} else {
fmt.Println("i")
}
}
}
27 . Section Review Dice Roller :
Go Package Registry .
https://pkg.go.dev/std
The
The first thing that we need to do is seed random number generation . Seed what it does is that it sets the initial value for the random numbers.
rand.Seed(time.Now().UnixNano())
- dice , sides := 2, 12 // number of dice that we have and number of sides that we have
- rolls := 1
Since we have multiple dice and multiple roles, we should have the rolls for the outer loop .
we will us r for the outer loop for the number of rolls . As long as it is less than the number of rolls we are going to keep going.
Comments
Post a Comment