Compare commits

..

No commits in common. "master" and "day-1" have entirely different histories.

7 changed files with 90 additions and 163 deletions

View File

@ -1,4 +0,0 @@
<?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"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/.idea/LearningGo.iml" filepath="$PROJECT_DIR$/.idea/LearningGo.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/restapi.iml" filepath="$PROJECT_DIR$/.idea/restapi.iml" />
</modules> </modules>
</component> </component>
</project> </project>

9
.idea/restapi.iml Normal file
View File

@ -0,0 +1,9 @@
<?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
View File

@ -1,100 +0,0 @@
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/LearningGo module git.nevets.tech/Steven/gorestapi
go 1.19 go 1.19

View File

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