Mode:
Duration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main
import "fmt"
type UserRepository interface {
GetUser(id int) string
}
type MockRepository struct{}
func (m MockRepository) GetUser(id int) string {
return "Alice"
}
type UserService struct {
repo UserRepository
}
func (s UserService) GetUserName(id int) string {
return s.repo.GetUser(id)
}
func main() {
service := UserService{repo: MockRepository{}}
fmt.Println(service.GetUserName(1))
}Coding works best on desktop or with an external keyboard.