Created
February 10, 2016 06:25
-
-
Save tkrajina/bf436c8fb6671ce64d0d to your computer and use it in GitHub Desktop.
Golang inject example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
"github.com/facebookgo/inject" | |
) | |
type EmailSender struct { | |
name string | |
} | |
func (es EmailSender) sendMail(subject string, to []string, body string) error { | |
fmt.Println("...") | |
return nil | |
} | |
func (es EmailSender) sendMailOrLogError(subject string, to []string, body string) { | |
fmt.Println("...") | |
} | |
type Test struct { | |
EmailSender *EmailSender `inject:""` | |
} | |
func main() { | |
emailSender := new(EmailSender) | |
emailSender.name = "this email sender" | |
test := new(Test) | |
started := float64(time.Now().UnixNano()) / float64(time.Millisecond) | |
err := inject.Populate(emailSender, test) | |
finished := float64(time.Now().UnixNano()) / float64(time.Millisecond) | |
if err != nil { | |
panic(err.Error()) | |
} | |
fmt.Println("time=", finished-started) | |
fmt.Println("test=", test) | |
fmt.Println("test.emailSender=", test.EmailSender) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment