Advertisement
Guest User

Untitled

a guest
Mar 13th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.96 KB | None | 0 0
  1. //Lectia 5 reference types + array-uri
  2.     {
  3.         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. ")
  4.         grades := []int{87, 55, 43, 71, 60, 43, 32, 19, 63} // grades este un slice deoarece nu are dimensiunea fixa
  5.         inflate(grades, 3)
  6.         fmt.Println(grades)
  7.  
  8.         arrayExample := [3]int{10,20,25}
  9.  
  10.         grades = arrayExample[:]
  11.         inflate2(grades, 3)
  12.         fmt.Println(grades)
  13.  
  14.         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 )
  15.         fmt.Println("Lungimea array-ului de mai sus:",len(arrayExampleEllipsisOperator))
  16.  
  17.  
  18.  
  19.  
  20.     }
  21.     //Lectia 6 Modificarea itemelor unui slice
  22.     {
  23.  
  24.         //A slice is a convenient, flexible and powerful wrapper on top of an array.
  25.         //Slices do not own any data on their own. They are the just references to existing arrays
  26.         s := []string{"A", "B", "C", "D", "E", "F", "G"}
  27.         t := s[:5] // [A B C D E]
  28.         u := s[3 : len(s)-1] // [D E F]
  29.         fmt.Println(s, t, u)
  30.         u[1] = "x"
  31.         //A slice does not own any data of its own. It is just a representation of the underlying array.
  32.         //Any modifications done to the slice will be reflected in the underlying array.
  33.         fmt.Println(s, t, u)
  34.  
  35.         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")
  36.     }
  37.     // Lectia 7 Slice-uri de structuri
  38.     {
  39.         products := []*Product{{"Spanner", 3.99}, {"Wrench", 2.49},
  40.             {"Screwdriver", 1.99}}
  41.         fmt.Println(products)
  42.         for _, product := range products {
  43.             product.price += 0.50 // capitalism
  44.         }
  45.         fmt.Println(products)
  46.     }
  47.  
  48.     //Lectia 8 Modificarea slice-urilor
  49.     {
  50.         s:= []string{"Ana ","Maria ","are ","mere "}
  51.         s = append(s,"multe")
  52.         s = s[1:]
  53.         fmt.Println(s)
  54.        
  55.        
  56.         fmt.Println("A slice can be re-sliced upto its capacity. Anything beyond that will cause the program to throw a run time error.")
  57.         fruitarray := [...]string{"apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}
  58.         fruitslice := fruitarray[1:3]
  59.         fmt.Printf("length of slice %d capacity %d\n", len(fruitslice), cap(fruitslice)) //length of is 2 and capacity is 6
  60.         fruitslice = fruitslice[:cap(fruitslice)] //re-slicing furitslice till its capacity
  61.         fmt.Println("After re-slicing length is",len(fruitslice), "and capacity is",cap(fruitslice))
  62.        
  63.        
  64.         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.")
  65.         cars := []string{"Ferrari", "Honda", "Ford"}
  66.         fmt.Println("cars:", cars, "has old length", len(cars), "and capacity", cap(cars)) //capacity of cars is 3
  67.         cars = append(cars, "Toyota")
  68.         fmt.Println("cars:", cars, "has new length", len(cars), "and capacity", cap(cars)) //capacity of cars is doubled to 6
  69.  
  70.     }
  71.  
  72.     //Lectia 9 Sortarea slice-urilor
  73.     {
  74.         names := []string{"Zana", "Sana", "Banana", "Babana", "Jabana"}
  75.         fmt.Printf("Nesortat: %q\n", names)
  76.         sort.Strings(names) // Standard library sort function
  77.         fmt.Printf("Sortat: %q\n", names)
  78.     }
  79.     //Lectia 10 Maps
  80.     {
  81.         planet := map[string]float64{}
  82.         planet["Earth"] = 1.25
  83.         planet["Mars"] = 0.87
  84.         planet["Jupiter"] = 5
  85.  
  86.         delete(planet, "Earth")
  87.  
  88.         jupiter := planet["Jupiter"]
  89.         venus, venusFound := planet["Venus"] // deoarece cheia venus nu exista, variabila venus va lua valoarea zero ( the zero value for the value type)
  90.         fmt.Println(jupiter,venus)
  91.         if venusFound {
  92.             println("Venus exists in the planet map")
  93.         }else{
  94.             println("Venus does not exists in the planet map")
  95.         }
  96.        
  97.         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.")
  98.        
  99.         personSalary := map[string]int{
  100.         "steve": 12000,
  101.         "jamie": 15000,
  102.         }
  103.         personSalary["mike"] = 9000
  104.         fmt.Println("Original person salary", personSalary)
  105.         newPersonSalary := personSalary
  106.         newPersonSalary["mike"] = 18000
  107.         fmt.Println("Person salary changed", personSalary)
  108.            
  109.         fmt.Println("Maps can't be compared using the == operator. The == can be only used to check if a map is nil.") 
  110.         map1 := map[string]int{
  111.             "one": 1,
  112.             "two": 2,
  113.         }
  114.  
  115.         map2 := map1
  116.  
  117.         if map1 == map2 {
  118.         }
  119.         //The above program will throw compilation error invalid operation: map1 == map2 (map can only be compared to nil).
  120.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement