Conclusion: the value does not guarantee that the address will be retrieved
Either s = &serviceImpl {} or s = &serviceImpl {} will work
type Service interface {
Login(username, password string) string
}
type ServiceImpl struct{}
func (s ServiceImpl) Login(username, password string) string {
return username + password
}
func main(a) {
var s Service
s = ServiceImpl{}
// s = &ServiceImpl{}
fmt.Println(s.Login("abc"."123"))}Copy the code
But if we change the receiver of ServiceImpl to a pointer type, the interface must be assigned a pointer type:
type Service interface {
Login(username, password string) string
}
type ServiceImpl struct{}
func (s *ServiceImpl) Login(username, password string) string {
return username + password
}
func main(a) {
var s Service
s = &ServiceImpl{}
fmt.Println(s.Login("abc"."123"))}Copy the code
There is no way to get an address for a type like the following:
type Time int
func main(a) {
fmt.Println(&Time(1))}Copy the code
Sure, you can get the address like this:
type Time int
func main(a) {
t := Time(1)
fmt.Println(&t)
}
Copy the code