English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang 기본 강의

Golang 제어 문

Golang 함수 & 메서드

Golang 구조체

Golang 슬라이스 & 배열

Golang 문자열(String)

Golang 포인터

Golang 인터페이스

Golang 병행

Golang 예외(Error)

Golang 다른 기타

Go 언어 다중 인터페이스

Go 언어에서 인터페이스는 메서드 서명의 집합으로, 또한 하나의 类型입니다. 이를 통해 인터페이스 타입의 변수를 생성할 수 있습니다. Go 언어에서는 다음과 같은 문법으로 여러 인터페이스를 생성할 수 있습니다:

type interface_name interface{
//메서드 서명
}

주의:Go 언어에서는 두 개나 더 많은 인터페이스에서 동일한 이름의 메서드를 생성할 수 없습니다. 이를 시도하면 프로그램이 쓰러질 것입니다. 여러 인터페이스에 대해 예제를 통해 논의해 보겠습니다.

//여러 인터페이스 개념
package main
import "fmt"
// 인터페이스 1
type AuthorDetails interface {
    details()
}
// 인터페이스 2
type AuthorArticles interface {
    articles()
}
// 구조체
type Author Articles interface {
    a_name
    학과
    대학
    year      int
    salary    int
    particles int
    tarticles int
}
//인터페이스 메서드 구현1
func (a author) details() {
    fmt.Printf("작가: %s", a.a_name)
    fmt.Printf("\n학과: %s 출판 일자: %d", a.branch, a.year)
    fmt.Printf("\n학교 이름: %s", a.college)
    fmt.Printf("\n급여: %d", a.salary)
    fmt.Printf("\n출판 기사 수: %d", a.particles)
}
// 인터페이스 메서드 구현 2
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\n예정된 기사: %d", pendingarticles)
}
// Main value
func main() {
    //구조체 할당
    values := author{
        a_name:    "Mickey",
        branch:    "Computer science",
        college:   "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
    }
    // 인터페이스를 사용하여 접근1의 메서드
    var i1 AuthorDetails = values
    i1.details()
    //인터페이스를 사용하여 접근2의 메서드
    var i2 AuthorArticles = values
    i2.articles()
}

출력:

작가: Mickey
학과: Computer science 출판 일자: 2012
학교 이름: XYZ
급여: 50000
출판 기사 수: 209
예정된 기사: 100

사용 설명:위 예제와 같이, 두 가지 메서드를 가진 인터페이스가 있습니다.那就是details()와Articles()。여기서 details() 메서드는 작가의 기본 정보를 제공하고 articles() 메서드는 작가의 예정된 기사를 제공합니다.

이름이 작가(Author)인 또 다른 구조가 있습니다. 이 구조는 인터페이스에서 사용되는 값이 포함된 변수 집합을 가지고 있습니다. 주요 메서드에서는, 이 구조에서 존재하는 변수의 값을 할당하여 인터페이스에서 사용하고 인터페이스 타입 변수를 생성하여 접근할 수 있도록 합니다.AuthorDetailsAuthorArticles인터페이스 메서드.