Home > Backend Development > Golang > Small Go interfaces example: proof of identity cards

Small Go interfaces example: proof of identity cards

Patricia Arquette
Release: 2025-01-12 11:01:41
Original
384 people have browsed it

Small Go interfaces example: proof of identity cards

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

  1. Create a project directory: mkdir proof-of-identity-checker && cd proof-of-identity-checker
  2. Initialize a Go module: go mod init <yourname>/proof-of-identity-checker (replace <yourname> with your name or a suitable identifier).
  3. Open the directory in your code editor.

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>
Copy after login

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>
Copy after login

Implementing Identity Document Types

  • Identification Card (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>
Copy after login
  • Driver's License (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>
Copy after login
  • Passport (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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template