packagelikesimport"fmt"//LikestakesasliceofpeoplewholikeapostandreturnsastringindicatingwholikesitfuncLikes(list[]string)string{likeLen:=len(list)switchlikeLen{case0:return"no one likes this"case1:returnfmt.Sprintf("%s likes this",list[0])case2:returnfmt.Sprintf("%s and %s like this",list[0],list[1])case3:returnfmt.Sprintf("%s, %s, and %s like this",list[0],list[1],list[2])default:returnfmt.Sprintf("%s, %s, and %d others like this",list[0],list[1],likeLen-2)}}
likes_test.go
packagelikesimport("testing")vartestCases=[]struct{descriptionstringinput[]stringexpectedstring}{{"no likes",[]string{},"no one likes this",},{"one like",[]string{"Mark"},"Mark likes this",},{"two likes",[]string{"Mark","Jeff"},"Mark and Jeff like this",},{"three likes",[]string{"Mark","Jeff","Bob"},"Mark, Jeff, and Bob like this",},{"many likes",[]string{"Mark","Jeff","Bob","Alice","Susan"},"Mark, Jeff, and 3 others like this",},}funcTestLikes(t*testing.T){for_,test:=rangetestCases{ifresult:=Likes(test.input);result!= test.expected {
t.Fatalf("FAIL: %s - Likes(%v): %s, expected: %s \n",test.description,test.input,result,test.expected)}t.Logf("PASS: %s \n",test.description)}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
More Go with tests:
likes.go
likes_test.go