End of day 2
This commit is contained in:
parent
0eea646621
commit
541982c248
4
.idea/LearningGo.iml
Normal file
4
.idea/LearningGo.iml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module version="4">
|
||||||
|
<component name="Go" enabled="true" />
|
||||||
|
</module>
|
@ -2,7 +2,7 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectModuleManager">
|
<component name="ProjectModuleManager">
|
||||||
<modules>
|
<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>
|
</modules>
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -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
100
dayone.go
Normal 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
2
go.mod
@ -1,3 +1,3 @@
|
|||||||
module git.nevets.tech/Steven/gorestapi
|
module git.nevets.tech/Steven/LearningGo
|
||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
10
goofin.go
Normal file
10
goofin.go
Normal 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
126
main.go
@ -1,106 +1,74 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
var globalVariable = "this is a global variable"
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
dataTypesExamples()
|
|
||||||
|
|
||||||
printingExamples()
|
fmt.Println("-------------------------ARRAYS-------------------------")
|
||||||
}
|
|
||||||
|
|
||||||
func printingExamples() {
|
// Arrays in GOLang start at 0!!!
|
||||||
age := 69
|
|
||||||
name := "Joe"
|
|
||||||
|
|
||||||
//Print
|
// Three ways to initialize arrays
|
||||||
fmt.Print("hello, ") // does NOT add a new line
|
// you can either define the length in the [x]type part
|
||||||
fmt.Print("world! \n") // add new line manually
|
// or let it determine the amount based on the number of entries given
|
||||||
fmt.Print("new line \n")
|
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(ages, len(moreAges), names) // len() function gets the length or size of array
|
||||||
fmt.Println("Hello on one line!") // automatically adds new line
|
|
||||||
fmt.Println("Good bye on one line!")
|
|
||||||
|
|
||||||
//Passing multiple values
|
// slices (use arrays under the hood)
|
||||||
fmt.Println("My age is", age, "and my name is", name)
|
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
|
fmt.Println(scores, len(scores))
|
||||||
// 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)
|
// slice ranges
|
||||||
var sprintfVar = fmt.Sprintf("My age is %v and my name is %v \n", age, name)
|
rangeOne := names[1:3]
|
||||||
fmt.Println("The saved string is:", sprintfVar)
|
|
||||||
}
|
|
||||||
|
|
||||||
func dataTypesExamples() {
|
fmt.Println(rangeOne)
|
||||||
|
|
||||||
// Date Types Doc https://go.dev/ref/spec#Types
|
fmt.Println("-------------------------STANDARD-LIBRARY-------------------------")
|
||||||
|
|
||||||
fmt.Println("-------------------------STRINGS-------------------------")
|
// strings package
|
||||||
// String docs https://go.dev/ref/spec#String_types
|
|
||||||
|
|
||||||
// Three ways to initialize a variable
|
greeting := "hello there"
|
||||||
var nameOne string = "mario"
|
fmt.Println(strings.Contains(greeting, "hello")) // strings.Contains returns a boolean value depending on if the passed value contains the second string
|
||||||
var nameTwo = "luigi"
|
fmt.Println(strings.Contains(greeting, "Hello")) // This function IS case-sensitive
|
||||||
var nameThree string
|
|
||||||
|
|
||||||
fmt.Println(nameOne)
|
fmt.Println(strings.ReplaceAll(greeting, "hello", "hi")) // Replaces all instances of the passed string
|
||||||
fmt.Println(nameTwo)
|
fmt.Println("Original string value is", greeting) // the original value is unchanged
|
||||||
fmt.Println(nameThree)
|
fmt.Println(strings.ToUpper(greeting)) // returns the passed string all uppercase
|
||||||
fmt.Println("All three: ", nameOne, nameTwo, nameThree)
|
|
||||||
|
|
||||||
nameOne = "peach"
|
fmt.Println(strings.Index(greeting, "ll")) // returns the first found position of the passed string
|
||||||
nameThree = "bowser"
|
|
||||||
fmt.Println(nameOne, nameTwo)
|
|
||||||
|
|
||||||
// Shorthand for variable initialization
|
fmt.Println(strings.Split(greeting, " ")) // splits first passed string into an array at each of the second passed variables instances
|
||||||
nameFour := "yoshi"
|
|
||||||
fmt.Println(nameFour)
|
|
||||||
|
|
||||||
// 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
|
sort.Ints(otherAges) // sorts given values AND stores it in the same variable
|
||||||
var ageOne int = 20
|
fmt.Println(otherAges)
|
||||||
var ageTwo = 30
|
|
||||||
ageThree := 40
|
|
||||||
|
|
||||||
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)
|
sort.Strings(otherNames)
|
||||||
var numOne int8 = 25 // -128 to 127
|
fmt.Println(otherNames)
|
||||||
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)
|
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
|
||||||
// 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)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user