Small Go インターフェイスの例: ID カードの証明
このチュートリアルでは、初心者向けに Go インターフェイスを説明します。身分証明書 (ID カード、運転免許証、パスポート) のメソッドを定義する ProofOfId
インターフェースと、国をリストするための CountriesList
インターフェースを作成します。 これは、インターフェイスが他の言語の継承を置き換えて、ポリモーフィズムの一形態としてどのように機能するかを示しています。
プロジェクトのセットアップ
- プロジェクトディレクトリを作成します:
mkdir proof-of-identity-checker && cd proof-of-identity-checker
- Go モジュールを初期化します:
go mod init <yourname>/proof-of-identity-checker
(<yourname>
を自分の名前または適切な識別子に置き換えます)。 - コードエディタでディレクトリを開きます。
インターフェースの定義 (interfaces.go
)
package main import "time" type ProofOfId interface { getExpDate() time.Time getName() string getObtentionDate() time.Time } type CountriesList interface { getCountries() []string }
ProofOfId
には getExpDate()
、getName()
、および getObtentionDate()
が必要です。 CountriesList
には getCountries()
が必要です。
インターフェースベースの関数 (main.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) } }
ID ドキュメントタイプの実装
- 身分証明書 (
idcard.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 }
- 運転免許証 (
driver-license.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 }
- パスポート (
passport.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 }
テスト (main.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) }
go run .
結論
この例は、Go インターフェースがオブジェクト間のコントラクトを定義し、コードの再利用性と保守性を促進することにより、どのように柔軟なコードを可能にするかを示しています。 IdentityVerification
関数の日付チェック ロジックを完了するには、日付比較用に提供されているリンクを参照してください。
以上がSmall Go インターフェイスの例: ID カードの証明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Golangは、パフォーマンスとスケーラビリティの点でPythonよりも優れています。 1)Golangのコンピレーションタイプの特性と効率的な並行性モデルにより、高い並行性シナリオでうまく機能します。 2)Pythonは解釈された言語として、ゆっくりと実行されますが、Cythonなどのツールを介してパフォーマンスを最適化できます。

Golangは並行性がCよりも優れていますが、Cは生の速度ではGolangよりも優れています。 1)Golangは、GoroutineとChannelを通じて効率的な並行性を達成します。これは、多数の同時タスクの処理に適しています。 2)Cコンパイラの最適化と標準ライブラリを介して、極端な最適化を必要とするアプリケーションに適したハードウェアに近い高性能を提供します。

goisidealforforbeginnersandsutable forcloudnetworkservicesduetoitssimplicity、andconcurrencyfeatures.1)installgofromtheofficialwebsiteandverify with'goversion'.2)

Golangは迅速な発展と同時シナリオに適しており、Cは極端なパフォーマンスと低レベルの制御が必要なシナリオに適しています。 1)Golangは、ごみ収集と並行機関のメカニズムを通じてパフォーマンスを向上させ、高配列Webサービス開発に適しています。 2)Cは、手動のメモリ管理とコンパイラの最適化を通じて究極のパフォーマンスを実現し、埋め込みシステム開発に適しています。

GolangとPythonにはそれぞれ独自の利点があります。Golangは高性能と同時プログラミングに適していますが、PythonはデータサイエンスとWeb開発に適しています。 Golangは同時性モデルと効率的なパフォーマンスで知られていますが、Pythonは簡潔な構文とリッチライブラリエコシステムで知られています。

GolangとCのパフォーマンスの違いは、主にメモリ管理、コンピレーションの最適化、ランタイム効率に反映されています。 1)Golangのゴミ収集メカニズムは便利ですが、パフォーマンスに影響を与える可能性があります。

GolangとCにはそれぞれパフォーマンス競争において独自の利点があります。1)Golangは、高い並行性と迅速な発展に適しており、2)Cはより高いパフォーマンスと微細な制御を提供します。選択は、プロジェクトの要件とチームテクノロジースタックに基づいている必要があります。

GolangisidealforBuildingsCalables Systemsduetoitsefficiency andConcurrency、Whilepythonexcelsinquickscriptinganddataanalysisduetoitssimplicityand vastecosystem.golang'ssignencouragesclean、readisinediteNeditinesinedinediseNabletinedinedinedisedisedioncourase
