End of day 2

This commit is contained in:
Steven Tracey 2022-08-24 14:26:32 -04:00
parent 0eea646621
commit 541982c248
7 changed files with 163 additions and 90 deletions

4
.idea/LearningGo.iml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="Go" enabled="true" />
</module>

View File

@ -2,7 +2,7 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/restapi.iml" filepath="$PROJECT_DIR$/.idea/restapi.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/LearningGo.iml" filepath="$PROJECT_DIR$/.idea/LearningGo.iml" />
</modules>
</component>
</project>

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

100
dayone.go Normal file
View File

@ -0,0 +1,100 @@
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)
}

2
go.mod
View File

@ -1,3 +1,3 @@
module git.nevets.tech/Steven/gorestapi
module git.nevets.tech/Steven/LearningGo
go 1.19

10
goofin.go Normal file
View File

@ -0,0 +1,10 @@
package main
func divider(divider string) {
endString := ""
totalLength := 50
length := len(divider)
//TODO make this return a fixed size divider!
}

126
main.go
View File

@ -1,106 +1,74 @@
package main
import "fmt"
var globalVariable = "this is a global variable"
import (
"fmt"
"sort"
"strings"
)
func main() {
dataTypesExamples()
printingExamples()
}
fmt.Println("-------------------------ARRAYS-------------------------")
func printingExamples() {
age := 69
name := "Joe"
// Arrays in GOLang start at 0!!!
//Print
fmt.Print("hello, ") // does NOT add a new line
fmt.Print("world! \n") // add new line manually
fmt.Print("new line \n")
// Three ways to initialize arrays
// you can either define the length in the [x]type part
// or let it determine the amount based on the number of entries given
var ages [3]int = [3]int{16, 17, 18}
var moreAges = [3]int{21, 22, 23}
names := [4]string{"mario", "luigi", "peach", "yoshi"}
//Println
fmt.Println("Hello on one line!") // automatically adds new line
fmt.Println("Good bye on one line!")
fmt.Println(ages, len(moreAges), names) // len() function gets the length or size of array
//Passing multiple values
fmt.Println("My age is", age, "and my name is", name)
// slices (use arrays under the hood)
var scores = []int{100, 50, 60}
scores[2] = 25
scores = append(scores, 85)
//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
fmt.Println(scores, len(scores))
// 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)
}
// slice ranges
rangeOne := names[1:3]
func dataTypesExamples() {
fmt.Println(rangeOne)
// Date Types Doc https://go.dev/ref/spec#Types
fmt.Println("-------------------------STANDARD-LIBRARY-------------------------")
fmt.Println("-------------------------STRINGS-------------------------")
// String docs https://go.dev/ref/spec#String_types
// strings package
// Three ways to initialize a variable
var nameOne string = "mario"
var nameTwo = "luigi"
var nameThree string
greeting := "hello there"
fmt.Println(strings.Contains(greeting, "hello")) // strings.Contains returns a boolean value depending on if the passed value contains the second string
fmt.Println(strings.Contains(greeting, "Hello")) // This function IS case-sensitive
fmt.Println(nameOne)
fmt.Println(nameTwo)
fmt.Println(nameThree)
fmt.Println("All three: ", nameOne, nameTwo, nameThree)
fmt.Println(strings.ReplaceAll(greeting, "hello", "hi")) // Replaces all instances of the passed string
fmt.Println("Original string value is", greeting) // the original value is unchanged
fmt.Println(strings.ToUpper(greeting)) // returns the passed string all uppercase
nameOne = "peach"
nameThree = "bowser"
fmt.Println(nameOne, nameTwo)
fmt.Println(strings.Index(greeting, "ll")) // returns the first found position of the passed string
// Shorthand for variable initialization
nameFour := "yoshi"
fmt.Println(nameFour)
fmt.Println(strings.Split(greeting, " ")) // splits first passed string into an array at each of the second passed variables instances
// Numeric data types doc https://go.dev/ref/spec#Numeric_types
// sort package
fmt.Println("-------------------------INTS-------------------------")
otherAges := []int{45, 20, 35, 30, 75, 60, 50, 25}
// ints
var ageOne int = 20
var ageTwo = 30
ageThree := 40
sort.Ints(otherAges) // sorts given values AND stores it in the same variable
fmt.Println(otherAges)
fmt.Println(ageOne, ageTwo, ageThree)
index := sort.SearchInts(otherAges, 30) // gets the index of the passed value from the array first passed
secondIndex := sort.SearchInts(otherAges, 90) // if the value passed is not found in the array, it will return what position it WOULD be in
thirdIndex := sort.SearchInts(otherAges, 55)
fmt.Println(index)
fmt.Println(secondIndex)
fmt.Println(thirdIndex)
// bits & memory
otherNames := []string{"mario", "luigi", "yoshi", "peach", "bowser"}
// 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)
sort.Strings(otherNames)
fmt.Println(otherNames)
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)
fmt.Println(sort.SearchStrings(otherNames, "bowser")) // gives the position of the passed string in the array if found
fmt.Println(sort.SearchStrings(otherNames, "waluigi")) // otherwise will return where it WOULD be
}