| 
									
										
										
										
											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" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() { | 
					
						
							|  |  |  | 	var ( | 
					
						
							|  |  |  | 		nums   []int | 
					
						
							|  |  |  | 		oldCap float64 | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 15:08:32 +03:00
										 |  |  | 	// loop 10 million times | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | 	for len(nums) < 10e6 { | 
					
						
							| 
									
										
										
										
											2019-08-18 15:08:32 +03:00
										 |  |  | 		// get the capacity | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | 		c := float64(cap(nums)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 15:08:32 +03:00
										 |  |  | 		// only print when the capacity changes | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | 		if c == 0 || c != oldCap { | 
					
						
							| 
									
										
										
										
											2019-08-18 15:08:32 +03:00
										 |  |  | 			// print also the growth ratio: c/oldCap | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | 			fmt.Printf("len:%-15d cap:%-15g growth:%-15.2f\n", | 
					
						
							|  |  |  | 				len(nums), c, c/oldCap) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-18 15:08:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// keep track of the previous capacity | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | 		oldCap = c | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 15:08:32 +03:00
										 |  |  | 		// append an arbitrary element to the slice | 
					
						
							| 
									
										
										
										
											2019-03-04 20:30:18 +03:00
										 |  |  | 		nums = append(nums, 1) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |