课程: Hands-On Introduction: Go

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Challenge 6: Refactoring with generics

Challenge 6: Refactoring with generics - Go教程

课程: Hands-On Introduction: Go

Challenge 6: Refactoring with generics

- This challenge will help cement what we've learned about generics in this course by having us refactor some non-generic functions into their generic counterparts. We'll open up the starting file for this challenge and let's take a look at the predefined print functions. PrintString, printInt, and printbool. Your first task is to write a generic print function that can work with any of the types of the non-generic versions, String, Int and Bool. Next note, the sum function and its use of the empty interface and the necessary tap switching that it needs to do internally in order to actually accumulate the numbers and return that. Your second task is to refactor the sum function into a generic one, and it must use a custom type constraint that you'll define called numeric. The numeric type constraint needs to include all sign and unsigned integers and floats. Hint, hint the constraints package can help you here…

内容