LearnGo/main.go
2022-08-24 14:26:32 -04:00

75 lines
2.5 KiB
Go

package main
import (
"fmt"
"sort"
"strings"
)
func main() {
fmt.Println("-------------------------ARRAYS-------------------------")
// Arrays in GOLang start at 0!!!
// 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"}
fmt.Println(ages, len(moreAges), names) // len() function gets the length or size of array
// slices (use arrays under the hood)
var scores = []int{100, 50, 60}
scores[2] = 25
scores = append(scores, 85)
fmt.Println(scores, len(scores))
// slice ranges
rangeOne := names[1:3]
fmt.Println(rangeOne)
fmt.Println("-------------------------STANDARD-LIBRARY-------------------------")
// strings package
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(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
fmt.Println(strings.Index(greeting, "ll")) // returns the first found position of the passed string
fmt.Println(strings.Split(greeting, " ")) // splits first passed string into an array at each of the second passed variables instances
// sort package
otherAges := []int{45, 20, 35, 30, 75, 60, 50, 25}
sort.Ints(otherAges) // sorts given values AND stores it in the same variable
fmt.Println(otherAges)
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)
otherNames := []string{"mario", "luigi", "yoshi", "peach", "bowser"}
sort.Strings(otherNames)
fmt.Println(otherNames)
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
}