Iteration done
							parent
							
								
									76f5fc442c
								
							
						
					
					
						commit
						715f2d8296
					
				@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					module iteration
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					go 1.21.0
 | 
				
			||||||
@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					package iteration
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func Repeat(charaters string, count int) string {
 | 
				
			||||||
 | 
						var repeated string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for i := 0; i < count; i++ {
 | 
				
			||||||
 | 
							repeated += charaters
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return repeated
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,41 @@
 | 
				
			|||||||
 | 
					package iteration
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestRepeat(t *testing.T) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("repeat simple letters", func(t *testing.T) {
 | 
				
			||||||
 | 
							repeated := Repeat("a", 6)
 | 
				
			||||||
 | 
							expected := "aaaaaa"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if repeated != expected {
 | 
				
			||||||
 | 
								t.Errorf("expected %q but got %q", expected, repeated)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("repeat words letters", func(t *testing.T) {
 | 
				
			||||||
 | 
							repeated := Repeat("dog", 5)
 | 
				
			||||||
 | 
							expected := "dogdogdogdogdog"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if repeated != expected {
 | 
				
			||||||
 | 
								t.Errorf("expected %q but got %q", expected, repeated)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// This test demonstrates benchmark testing https://pkg.go.dev/testing#hdr-Benchmarks
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func BenchmarkRepeat(b *testing.B) {
 | 
				
			||||||
 | 
						for i := 0; i < b.N; i++ {
 | 
				
			||||||
 | 
							Repeat("a", 4)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ExampleRepeat() {
 | 
				
			||||||
 | 
						repeated := Repeat("da", 5)
 | 
				
			||||||
 | 
						fmt.Println(repeated)
 | 
				
			||||||
 | 
						// Output: dadadadada
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
					Loading…
					
					
				
		Reference in New Issue