Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lectia 5 reference types + array-uri
- {
- fmt.Println("A slice contains the length, capacity and a pointer to the zeroth element of the array. When a slice is passed to a function, even though it's passed by value, the pointer variable will refer to the same underlying array. ")
- grades := []int{87, 55, 43, 71, 60, 43, 32, 19, 63} // grades este un slice deoarece nu are dimensiunea fixa
- inflate(grades, 3)
- fmt.Println(grades)
- arrayExample := [3]int{10,20,25}
- grades = arrayExample[:]
- inflate2(grades, 3)
- fmt.Println(grades)
- arrayExampleEllipsisOperator := [...]int{1,5,23,5,6,23,45,7,34,6,4,5,4,2,2,5,67,23,345,3,23,4,5,2,34} // ... = ellipsis operator -> calculeaza lungimea array-ului :) ( array nu slice )
- fmt.Println("Lungimea array-ului de mai sus:",len(arrayExampleEllipsisOperator))
- }
- //Lectia 6 Modificarea itemelor unui slice
- {
- //A slice is a convenient, flexible and powerful wrapper on top of an array.
- //Slices do not own any data on their own. They are the just references to existing arrays
- s := []string{"A", "B", "C", "D", "E", "F", "G"}
- t := s[:5] // [A B C D E]
- u := s[3 : len(s)-1] // [D E F]
- fmt.Println(s, t, u)
- u[1] = "x"
- //A slice does not own any data of its own. It is just a representation of the underlying array.
- //Any modifications done to the slice will be reflected in the underlying array.
- fmt.Println(s, t, u)
- fmt.Println("Since the slices s, t, and u all refer to the same underlying data, a change to one will affect any of the others that refer to the same data")
- }
- // Lectia 7 Slice-uri de structuri
- {
- products := []*Product{{"Spanner", 3.99}, {"Wrench", 2.49},
- {"Screwdriver", 1.99}}
- fmt.Println(products)
- for _, product := range products {
- product.price += 0.50 // capitalism
- }
- fmt.Println(products)
- }
- //Lectia 8 Modificarea slice-urilor
- {
- s:= []string{"Ana ","Maria ","are ","mere "}
- s = append(s,"multe")
- s = s[1:]
- fmt.Println(s)
- fmt.Println("A slice can be re-sliced upto its capacity. Anything beyond that will cause the program to throw a run time error.")
- fruitarray := [...]string{"apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}
- fruitslice := fruitarray[1:3]
- fmt.Printf("length of slice %d capacity %d\n", len(fruitslice), cap(fruitslice)) //length of is 2 and capacity is 6
- fruitslice = fruitslice[:cap(fruitslice)] //re-slicing furitslice till its capacity
- fmt.Println("After re-slicing length is",len(fruitslice), "and capacity is",cap(fruitslice))
- fmt.Println("One question might be bothering you though. If slices are backed by arrays and arrays themselves are of fixed length then how come a slice is of dynamic length. Well what happens under the hoods is, when new elements are appended to the slice, a new array is created. The elements of the existing array are copied to this new array and a new slice reference for this new array is returned. The capacity of the new slice is now twice that of the old slice. Pretty cool right :). The following program will make things clear.")
- cars := []string{"Ferrari", "Honda", "Ford"}
- fmt.Println("cars:", cars, "has old length", len(cars), "and capacity", cap(cars)) //capacity of cars is 3
- cars = append(cars, "Toyota")
- fmt.Println("cars:", cars, "has new length", len(cars), "and capacity", cap(cars)) //capacity of cars is doubled to 6
- }
- //Lectia 9 Sortarea slice-urilor
- {
- names := []string{"Zana", "Sana", "Banana", "Babana", "Jabana"}
- fmt.Printf("Nesortat: %q\n", names)
- sort.Strings(names) // Standard library sort function
- fmt.Printf("Sortat: %q\n", names)
- }
- //Lectia 10 Maps
- {
- planet := map[string]float64{}
- planet["Earth"] = 1.25
- planet["Mars"] = 0.87
- planet["Jupiter"] = 5
- delete(planet, "Earth")
- jupiter := planet["Jupiter"]
- venus, venusFound := planet["Venus"] // deoarece cheia venus nu exista, variabila venus va lua valoarea zero ( the zero value for the value type)
- fmt.Println(jupiter,venus)
- if venusFound {
- println("Venus exists in the planet map")
- }else{
- println("Venus does not exists in the planet map")
- }
- fmt.Println("Similar to slices, maps are reference types. When a map is assigned to a new variable, they both point to the same internal data structure. Hence changes made in one will reflect in the other.")
- personSalary := map[string]int{
- "steve": 12000,
- "jamie": 15000,
- }
- personSalary["mike"] = 9000
- fmt.Println("Original person salary", personSalary)
- newPersonSalary := personSalary
- newPersonSalary["mike"] = 18000
- fmt.Println("Person salary changed", personSalary)
- fmt.Println("Maps can't be compared using the == operator. The == can be only used to check if a map is nil.")
- map1 := map[string]int{
- "one": 1,
- "two": 2,
- }
- map2 := map1
- if map1 == map2 {
- }
- //The above program will throw compilation error invalid operation: map1 == map2 (map can only be compared to nil).
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement