本教程为初学者演示了 Go 接口。我们将创建一个 ProofOfId
接口来定义身份证件(身份证、驾照、护照)的方法,以及一个用于列出国家/地区的 CountriesList
接口。 这说明了接口如何以多态性的形式发挥作用,取代其他语言中的继承。
项目设置
mkdir proof-of-identity-checker && cd proof-of-identity-checker
go mod init <yourname>/proof-of-identity-checker
(将 <yourname>
替换为您的姓名或合适的标识符)。定义接口 (interfaces.go
)
<code class="language-go">package main import "time" type ProofOfId interface { getExpDate() time.Time getName() string getObtentionDate() time.Time } type CountriesList interface { getCountries() []string }</code>
ProofOfId
需要 getExpDate()
、getName()
和 getObtentionDate()
。 CountriesList
需要 getCountries()
。
基于接口的函数 (main.go
)
<code class="language-go">package main import "time" // IdentityVerification checks if a proof of ID is valid (date-based). func IdentityVerification(proof ProofOfId) bool { // ... (Date comparison logic would go here. See the provided link for details.) return proof.getExpDate().After(time.Now()) //Example: Check if expiration date is in the future. } // DisplayVisitedCountries prints a list of visited countries. func DisplayVisitedCountries(passport CountriesList) { countries := passport.getCountries() println("Visited countries:") for _, country := range countries { println(country) } }</code>
实施身份证明文件类型
idcard.go
)<code class="language-go">package main import "time" type IdCard struct { Name string ObtentionDate time.Time ExpDate time.Time } func (card IdCard) getName() string { return card.Name } func (card IdCard) getExpDate() time.Time { return card.ExpDate } func (card IdCard) getObtentionDate() time.Time { return card.ObtentionDate }</code>
driver-license.go
)<code class="language-go">package main import "time" type DriverLicense struct { Name string ObtentionDate time.Time ExpDate time.Time Enterprise string } func (license DriverLicense) getName() string { return license.Name } func (license DriverLicense) getExpDate() time.Time { return license.ExpDate } func (license DriverLicense) getObtentionDate() time.Time { return license.ObtentionDate } func (license DriverLicense) getEnterprise() string { return license.Enterprise }</code>
passport.go
)<code class="language-go">package main import "time" type Passport struct { Name string ObtentionDate time.Time ExpDate time.Time VisitedCountries []string } func (passport Passport) getName() string { return passport.Name } func (passport Passport) getExpDate() time.Time { return passport.ExpDate } func (passport Passport) getObtentionDate() time.Time { return passport.ObtentionDate } func (passport Passport) getCountries() []string { return passport.VisitedCountries }</code>
测试(main.go
继续)
<code class="language-go">func main() { idCard := IdCard{ObtentionDate: time.Now().Add(24 * time.Hour), ExpDate: time.Now().Add(730 * 24 * time.Hour)} driverLicense := DriverLicense{ObtentionDate: time.Now().Add(-24 * time.Hour), ExpDate: time.Now().Add(365 * 24 * time.Hour)} passport := Passport{ObtentionDate: time.Now().Add(-24 * time.Hour), ExpDate: time.Now().Add(-1 * time.Hour), VisitedCountries: []string{"France", "Spain", "Belgium"}} println(IdentityVerification(idCard)) println(IdentityVerification(driverLicense)) println(IdentityVerification(passport)) DisplayVisitedCountries(passport) }</code>
与go run .
结论
这个示例展示了 Go 接口如何通过定义对象之间的契约来实现灵活的代码,从而提高代码的可重用性和可维护性。 应参考提供的日期比较链接来完成 IdentityVerification
函数的日期检查逻辑。
以上是小Go接口示例:身份证证明的详细内容。更多信息请关注PHP中文网其他相关文章!