| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | // For more tutorials: https://blog.learngoprogramming.com | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Copyright © 2018 Inanc Gumus | 
					
						
							|  |  |  | // Learn Go Programming Course | 
					
						
							|  |  |  | // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | 	"github.com/inancgumus/learngo/16-slices/exercises/23-limit-the-backing-array-sharing/api" | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // --------------------------------------------------------- | 
					
						
							|  |  |  | // EXERCISE: Limit the backing array sharing | 
					
						
							|  |  |  | // | 
					
						
							| 
									
										
										
										
											2019-08-23 10:19:50 +03:00
										 |  |  | //  GOAL | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2019-08-23 10:19:50 +03:00
										 |  |  | //    Limit the capacity of the slice that is returned | 
					
						
							|  |  |  | //    from the `Read` function. Read on for more details. | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | // | 
					
						
							|  |  |  | // | 
					
						
							| 
									
										
										
										
											2019-08-23 10:19:50 +03:00
										 |  |  | //  BEFORE YOU START | 
					
						
							| 
									
										
										
										
											2019-08-22 21:21:26 +03:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2019-08-23 10:19:50 +03:00
										 |  |  | //    In this exercise: API means the api package. It's in the | 
					
						
							|  |  |  | //    api folder. You need to change the code in the `api/api.go` | 
					
						
							|  |  |  | //    to solve this exercise, and you need import the api | 
					
						
							|  |  |  | //    package. | 
					
						
							| 
									
										
										
										
											2019-08-18 12:59:50 +03:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2019-08-23 10:19:50 +03:00
										 |  |  | //  WHAT IS THE PROBLEM? | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2019-08-23 10:19:50 +03:00
										 |  |  | //    `Read` function of the api package returns a portion of | 
					
						
							|  |  |  | //    its `temps` slice. Below, `main()` saves it to the | 
					
						
							|  |  |  | //    `received` slice. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | //    `main()` appends to the `received` slice but doing so | 
					
						
							|  |  |  | //    also changes the backing array of the `temps` slice. | 
					
						
							|  |  |  | //    We don't want that. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | //    `main()` can change the part of the `temps` slice | 
					
						
							|  |  |  | //    that is returned from the `Read()`, but it shouldn't | 
					
						
							|  |  |  | //    be able to change the elements in the rest of the | 
					
						
							|  |  |  | //    `temps`. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | //  WHAT YOU NEED TO DO? | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | //    So you need to limit the capacity of the returned | 
					
						
							|  |  |  | //    slice somehow. Remember: `received` and `temps` | 
					
						
							|  |  |  | //    share the same backing array. So, appending to it | 
					
						
							|  |  |  | //    can overwrite the same backing array. | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | // | 
					
						
							|  |  |  | // | 
					
						
							| 
									
										
										
										
											2019-08-18 12:59:50 +03:00
										 |  |  | // CURRENT | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | // | 
					
						
							|  |  |  | //                           | | | 
					
						
							| 
									
										
										
										
											2019-08-18 12:59:50 +03:00
										 |  |  | //                           v v | 
					
						
							|  |  |  | //   api.temps     : [5 10 3 1 3 80 90] | 
					
						
							| 
									
										
										
										
											2019-08-22 21:21:26 +03:00
										 |  |  | //   main.received : [5 10 3 1 3] | 
					
						
							|  |  |  | //                           ^ ^ append changes the `temps` | 
					
						
							|  |  |  | //                               slice's backing array. | 
					
						
							| 
									
										
										
										
											2019-08-18 12:59:50 +03:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | // | 
					
						
							|  |  |  | // | 
					
						
							| 
									
										
										
										
											2019-08-18 12:59:50 +03:00
										 |  |  | // EXPECTED | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2019-08-22 21:21:26 +03:00
										 |  |  | //   The corrected api package does not allow the `main()` to | 
					
						
							|  |  |  | //   change unreturned portion of the temps slice's backing array. | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | //                           |  | | 
					
						
							| 
									
										
										
										
											2019-08-18 12:59:50 +03:00
										 |  |  | //                           v  v | 
					
						
							|  |  |  | //   api.temps     : [5 10 3 25 45 80 90] | 
					
						
							| 
									
										
										
										
											2019-08-22 21:21:26 +03:00
										 |  |  | //   main.received : [5 10 3 1 3] | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | // | 
					
						
							|  |  |  | // --------------------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() { | 
					
						
							| 
									
										
										
										
											2019-08-22 20:00:58 +03:00
										 |  |  | 	// DO NOT CHANGE ANYTHING IN THIS CODE. | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 12:59:50 +03:00
										 |  |  | 	// get the first three elements from api.temps | 
					
						
							| 
									
										
										
										
											2019-08-22 21:21:26 +03:00
										 |  |  | 	received := api.Read(0, 3) | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-22 20:00:58 +03:00
										 |  |  | 	// append changes the api package's temps slice's | 
					
						
							|  |  |  | 	// backing array as well. | 
					
						
							| 
									
										
										
										
											2019-08-22 21:21:26 +03:00
										 |  |  | 	received = append(received, []int{1, 3}...) | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 12:59:50 +03:00
										 |  |  | 	fmt.Println("api.temps     :", api.All()) | 
					
						
							| 
									
										
										
										
											2019-08-22 21:21:26 +03:00
										 |  |  | 	fmt.Println("main.received :", received) | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | } |