本教學為初學者示範了 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中文網其他相關文章!