| 
									
										
										
										
											2019-09-01 21:57:52 +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" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 17:03:19 +03:00
										 |  |  | type item interface { | 
					
						
							| 
									
										
										
										
											2019-09-27 11:31:46 +03:00
										 |  |  | 	discount(ratio float64) | 
					
						
							| 
									
										
										
										
											2019-09-02 17:03:19 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-27 19:09:07 +03:00
										 |  |  | 	// ~~ interface embedding ~~ | 
					
						
							|  |  |  | 	// | 
					
						
							| 
									
										
										
										
											2019-09-02 17:03:19 +03:00
										 |  |  | 	// item interface embeds the fmt.Stringer interface. | 
					
						
							|  |  |  | 	// | 
					
						
							| 
									
										
										
										
											2019-09-27 19:09:07 +03:00
										 |  |  | 	// Go adds the methods of the fmt.Stringer | 
					
						
							|  |  |  | 	// to this item interface. | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// same as this: | 
					
						
							| 
									
										
										
										
											2019-09-02 17:03:19 +03:00
										 |  |  | 	// String() string | 
					
						
							| 
									
										
										
										
											2019-10-15 13:30:43 +03:00
										 |  |  | 	// fmt.Stringer | 
					
						
							| 
									
										
										
										
											2019-09-02 17:03:19 +03:00
										 |  |  | } | 
					
						
							|  |  |  | type list []item | 
					
						
							| 
									
										
										
										
											2019-09-01 21:57:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-27 19:09:07 +03:00
										 |  |  | // String method makes the list an fmt.Stringer. | 
					
						
							| 
									
										
										
										
											2019-09-01 21:57:52 +03:00
										 |  |  | func (l list) String() string { | 
					
						
							|  |  |  | 	if len(l) == 0 { | 
					
						
							|  |  |  | 		return "Sorry. We're waiting for delivery 🚚." | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-27 19:09:07 +03:00
										 |  |  | 	// use the strings.Builder when you're combining | 
					
						
							|  |  |  | 	// a long list of strings together. | 
					
						
							| 
									
										
										
										
											2019-09-01 21:57:52 +03:00
										 |  |  | 	var str strings.Builder | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, it := range l { | 
					
						
							| 
									
										
										
										
											2019-09-27 19:09:07 +03:00
										 |  |  | 		// the builder.WriteString doesn't know about the stringer interface. | 
					
						
							|  |  |  | 		// because it takes a string argument. | 
					
						
							|  |  |  | 		// so, you need to call the String method yourself. | 
					
						
							| 
									
										
										
										
											2019-10-15 13:30:43 +03:00
										 |  |  | 		// str.WriteString(it.String()) | 
					
						
							|  |  |  | 		// str.WriteRune('\n') | 
					
						
							| 
									
										
										
										
											2019-09-27 19:09:07 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// or slower way: | 
					
						
							| 
									
										
										
										
											2019-10-15 13:30:43 +03:00
										 |  |  | 		// Print funcs can detect fmt.Stringer automatically. | 
					
						
							|  |  |  | 		fmt.Fprintln(&str, it) | 
					
						
							| 
									
										
										
										
											2019-09-01 21:57:52 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return str.String() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (l list) discount(ratio float64) { | 
					
						
							|  |  |  | 	for _, it := range l { | 
					
						
							| 
									
										
										
										
											2019-09-02 17:03:19 +03:00
										 |  |  | 		it.discount(ratio) | 
					
						
							| 
									
										
										
										
											2019-09-01 21:57:52 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |