From 541982c248eb1c6c2bed7fc6a70bb5e5ccb1f19e Mon Sep 17 00:00:00 2001 From: Steven Tracey Date: Wed, 24 Aug 2022 14:26:32 -0400 Subject: [PATCH] End of day 2 --- .idea/LearningGo.iml | 4 ++ .idea/modules.xml | 2 +- .idea/restapi.iml | 9 ---- dayone.go | 100 ++++++++++++++++++++++++++++++++++ go.mod | 2 +- goofin.go | 10 ++++ main.go | 126 ++++++++++++++++--------------------------- 7 files changed, 163 insertions(+), 90 deletions(-) create mode 100644 .idea/LearningGo.iml delete mode 100644 .idea/restapi.iml create mode 100644 dayone.go create mode 100644 goofin.go diff --git a/.idea/LearningGo.iml b/.idea/LearningGo.iml new file mode 100644 index 0000000..7ee078d --- /dev/null +++ b/.idea/LearningGo.iml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index e0a39ef..c5c4725 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/restapi.iml b/.idea/restapi.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/restapi.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/dayone.go b/dayone.go new file mode 100644 index 0000000..d784c4a --- /dev/null +++ b/dayone.go @@ -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) + +} diff --git a/go.mod b/go.mod index 17f43cb..51cea5d 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module git.nevets.tech/Steven/gorestapi +module git.nevets.tech/Steven/LearningGo go 1.19 diff --git a/goofin.go b/goofin.go new file mode 100644 index 0000000..1ae9528 --- /dev/null +++ b/goofin.go @@ -0,0 +1,10 @@ +package main + +func divider(divider string) { + endString := "" + totalLength := 50 + length := len(divider) + + //TODO make this return a fixed size divider! + +} diff --git a/main.go b/main.go index feda23e..97504e5 100644 --- a/main.go +++ b/main.go @@ -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 }