101 lines
3.3 KiB
Go
101 lines
3.3 KiB
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
var globalVariable = "this is a global variable"
|
|
|
|
func printingExamples() {
|
|
age := 69
|
|
name := "Joe"
|
|
|
|
//Print
|
|
fmt.Print("hello, ") // does NOT add a new line
|
|
fmt.Print("world! \n") // add new line manually
|
|
fmt.Print("new line \n")
|
|
|
|
//Println
|
|
fmt.Println("Hello on one line!") // automatically adds new line
|
|
fmt.Println("Good bye on one line!")
|
|
|
|
//Passing multiple values
|
|
fmt.Println("My age is", age, "and my name is", name)
|
|
|
|
//Printf (formatted strings) %_ = format specifier - List of specifiers https://pkg.go.dev/fmt
|
|
// Does NOT add a new line like fmt.Print()
|
|
fmt.Printf("my age is %v and my name is %v \n", age, name) // %v takes any type of variables in the order they are provided
|
|
fmt.Printf("My age is %q and my name is %q \n", age, name) // %q adds quotes around the variables in the order they are provided, must be strings!
|
|
fmt.Printf("age is of type %T \n", age) // %T gets the type of the first variable passed after string to be printed
|
|
fmt.Printf("You scored %f points! \n", 225.55) // %f passes a float value passed
|
|
fmt.Printf("You scored %0.1f points! \n", 225.55) // %X.Xf rounds the passed float to the specified length
|
|
|
|
// Sprintf (saved formatted strings)
|
|
var sprintfVar = fmt.Sprintf("My age is %v and my name is %v \n", age, name)
|
|
fmt.Println("The saved string is:", sprintfVar)
|
|
}
|
|
|
|
func dataTypesExamples() {
|
|
|
|
// Date Types Doc https://go.dev/ref/spec#Types
|
|
|
|
fmt.Println("-------------------------STRINGS-------------------------")
|
|
// String docs https://go.dev/ref/spec#String_types
|
|
|
|
// Three ways to initialize a variable
|
|
var nameOne string = "mario"
|
|
var nameTwo = "luigi"
|
|
var nameThree string
|
|
|
|
fmt.Println(nameOne)
|
|
fmt.Println(nameTwo)
|
|
fmt.Println(nameThree)
|
|
fmt.Println("All three: ", nameOne, nameTwo, nameThree)
|
|
|
|
nameOne = "peach"
|
|
nameThree = "bowser"
|
|
fmt.Println(nameOne, nameTwo)
|
|
|
|
// Shorthand for variable initialization
|
|
nameFour := "yoshi"
|
|
fmt.Println(nameFour)
|
|
|
|
// Numeric data types doc https://go.dev/ref/spec#Numeric_types
|
|
|
|
fmt.Println("-------------------------INTS-------------------------")
|
|
|
|
// ints
|
|
var ageOne int = 20
|
|
var ageTwo = 30
|
|
ageThree := 40
|
|
|
|
fmt.Println(ageOne, ageTwo, ageThree)
|
|
|
|
// bits & memory
|
|
|
|
// signed (positive and negative)
|
|
var numOne int8 = 25 // -128 to 127
|
|
var numTwo int16 = 35 // -32768 to 32767 (-32,768 to 32,767)
|
|
var numThree int32 = 45 // -2147483648 to 2147483647 (-2,147,483,648 to 2,147,483,647)
|
|
var numThreeAndAHalf rune = 45 // rune is the same as int32
|
|
var numFour int64 = 55 // -9223372036854775808 to 9223372036854775807 (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
|
|
|
|
fmt.Println(numOne, numTwo, numThree, numThreeAndAHalf, numFour)
|
|
|
|
// unsigned (all positive)
|
|
var posNumOne uint8 = 65 // 0 to 255
|
|
var posNumTwo uint16 = 75 // 0 to 65535 (65,535)
|
|
var posNumThree uint32 = 85 // 0 to 4294967295 (4,294,967,295)
|
|
var posNumFour uint64 = 95 // 0 to 18446744073709551615 (18,446,744,073,709,551,615)
|
|
|
|
fmt.Println(posNumOne, posNumTwo, posNumThree, posNumFour)
|
|
|
|
fmt.Println("-------------------------FLOATS-------------------------")
|
|
|
|
// decimal values default to float64
|
|
var floatOne float32 = 25.98
|
|
var floatTwo float64 = 9843216984654.68432168454651968
|
|
floatThree := 6854324.6854654
|
|
|
|
fmt.Println(floatOne, floatTwo, floatThree)
|
|
|
|
}
|