This tutorial demonstrates Go interfaces for beginners. We'll create a ProofOfId
interface defining methods for identity documents (ID card, driver's license, passport) and a CountriesList
interface for listing countries. This illustrates how interfaces function as a form of polymorphism, replacing inheritance in other languages.
Project Setup
mkdir proof-of-identity-checker && cd proof-of-identity-checker
go mod init <yourname>/proof-of-identity-checker
(replace <yourname>
with your name or a suitable identifier).Defining Interfaces (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
requires getExpDate()
, getName()
, and getObtentionDate()
. CountriesList
requires getCountries()
.
Interface-Based Functions (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>
Implementing Identity Document Types
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>
Testing (main.go
continued)
<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>
Run with go run .
Conclusion
This example shows how Go interfaces enable flexible code by defining contracts between objects, promoting code reusability and maintainability. The provided link for date comparison should be consulted to complete the IdentityVerification
function's date checking logic.
The above is the detailed content of Small Go interfaces example: proof of identity cards. For more information, please follow other related articles on the PHP Chinese website!