Skip to Content
Go 소개
book

Go 소개

by Caleb Doxsey
May 2025
Beginner to intermediate
124 pages
1h 15m
Korean
O'Reilly Media, Inc.
Content preview from Go 소개

7장. 구조 및 인터페이스

이 작품은 AI를 사용하여 번역되었습니다. 여러분의 피드백과 의견을 환영합니다: translation-feedback@oreilly.com

Go에 내장된 데이터 유형만 사용하여 프로그램을 작성할 수도 있겠지만, 어느 시점에서는 상당히 지루해질 것입니다. 도형과 상호작용하는 프로그램을 생각해 봅시다:

package main

import ("fmt"; "math")

func distance(x1, y1, x2, y2 float64) float64 {
    a := x2 – x1
    b := y2 – y1
    return math.Sqrt(a*a + b*b)
}

func rectangleArea(x1, y1, x2, y2 float64) float64 {
    l := distance(x1, y1, x1, y2)
    w := distance(x1, y1, x2, y1)
    return l * w
}

func circleArea(x, y, r float64) float64 {
    return math.Pi * r*r
}

func main() {
    var rx1, ry1 float64 = 0, 0
    var rx2, ry2 float64 = 10, 10
    var cx, cy, cr float64 = 0, 0, 5

    fmt.Println(rectangleArea(rx1, ry1, rx2, ry2))
    fmt.Println(circleArea(cx, cy, cr))
}

이 프로그램은 직사각형과 원의 넓이를 구합니다. 모든 좌표를 추적하면 프로그램이 수행하는 작업을 확인하기가 어렵고 실수가 발생할 가능성이 높습니다.

구조

이 프로그램을 개선하는 쉬운 방법은 구조체를 사용하는 것입니다. 구조체는 명명된 필드를 포함하는 유형입니다. 예를 들어 다음과 같이 원을 표현할 수 있습니다:

type Circle struct {
    x float64
    y float64
    r float64
}

type 키워드는 새 유형을 소개합니다. 그 뒤에는 유형 이름 (Circle), struct 유형을 정의하고 있음을 나타내는 struct 키워드, 중괄호 안의 필드 목록이 이어집니다.

필드는 그룹화된 변수 집합과 같습니다. 각 필드에는 이름과 유형이 있으며 구조체에서 다른 필드와 인접하여 저장됩니다. 함수와 마찬가지로 동일한 유형을 가진 필드를 접을 수 있습니다:

type Circle struct {
    x, y, r float64
}

초기화

새로운 Circle 유형의 인스턴스를 다양한 방법으로 만들 수 있습니다:

var c Circle

다른 데이터 유형과 마찬가지로 기본적으로 0으로 설정된 로컬 Circle 변수가 생성됩니다. struct 의 경우 0은 각 필드가 해당 0 값으로 설정됨을 의미합니다( int의 경우0, float의 경우 0.0, "" 의 경우 string, 포인터의 경우 nil 등) new 함수를 사용할 수도 있습니다:

c := new(Circle)

이렇게 하면 모든 필드에 메모리를 할당하고 각 필드를 0 값으로 설정한 다음 구조체에 대한 포인터를 반환합니다(*Circle). 포인터는 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

효율적인 Go

효율적인 Go

Bartlomiej Plotka
핸즈온 머신러닝(3판)

핸즈온 머신러닝(3판)

오렐리앙 제롱(Aurélien Géron)
러닝 랭체인

러닝 랭체인

메이오 오신, 누노 캄포스
Go의 동시성

Go의 동시성

Katherine Cox-Buday

Publisher Resources

ISBN: 9798341659827