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 소개

4장. 제어 구조

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

이제 변수를 사용하는 방법을 알았으니 이제 몇 가지 유용한 프로그램을 작성해 보겠습니다. 먼저 1부터 시작하여 10까지 계산하는 프로그램을 각 숫자를 한 줄로 작성해 보겠습니다. 지금까지 배운 내용을 사용하여 다음과 같이 작성할 수 있습니다:

package main

import "fmt"

func main() {
    fmt.Println(1)
    fmt.Println(2)
    fmt.Println(3)
    fmt.Println(4)
    fmt.Println(5)
    fmt.Println(6)
    fmt.Println(7)
    fmt.Println(8)
    fmt.Println(9)
    fmt.Println(10)
}

아니면 이렇게 하세요:

package main
import "fmt"

func main() {
  fmt.Println(`1
2
3
4
5
6
7
8
9
10`)
}

하지만 이 두 프로그램은 모두 작성하기가 꽤 지루합니다. 우리에게 필요한 것은 무언가를 여러 번 수행할 수 있는 방법입니다.

for 문

for 문을 사용하면 문 목록(블록)을 여러 번 반복할 수 있습니다. 문을 사용하여 이전 프로그램을 다시 작성하면 다음과 같습니다: for

package main

import "fmt"

func main() {
    i := 1
    for i <= 10 {
        fmt.Println(i)
        i = i + 1
    }
}

먼저 인쇄할 숫자를 저장하는 데 사용할 i 이라는 변수를 만듭니다. 그런 다음 for 키워드를 사용하여 true 또는 false 조건 표현식을 제공하고 마지막으로 실행할 블록을 제공하여 for 루프를 만듭니다. for 루프는 다음과 같이 작동합니다:

  1. i <= 10 ("i가 10보다 작거나 같음") 표현식을 평가(실행)합니다. 참으로 평가되면 블록 내부의 문을 실행합니다. 그렇지 않으면 블록 뒤에 있는 프로그램의 다음 줄로 이동합니다(이 경우 for 루프 뒤에 아무 것도 없으므로 프로그램을 종료합니다).

  2. 블록 내부의 문을 실행한 후 for 문의 시작 부분으로 돌아가서 1단계를 반복합니다.

i = i + 1 라인은 매우 중요한데, 이 라인이 없으면 i <= 10 이 항상 true 로 평가되어 프로그램이 멈추지 않기 때문입니다(이런 경우를 무한 루프라고 합니다).

연습 삼아 컴퓨터처럼 프로그램을 살펴봅시다:

  1. i 이라는 변수를 1이라는 값으로 만듭니다.

  2. i <= 10? 예.

  3. 인쇄 i.

  4. i 을 i + 1 으로 설정합니다(i 은 이제 2와 같습니다).

  5. i <= 10? 예.

  6. 인쇄 i.

  7. i 을 i + 1 으로 설정합니다(i 은 이제 3과 같습니다).

  8. ...

  9. i 을 i + 1 으로 설정합니다(i 은 이제 11과 같습니다).

  10. i <= 10? 아니요.

  11. 남은 작업이 없으므로 종료합니다.

다른 프로그래밍 언어에는 다양한 유형의 루프(while, do, until, foreach, ...)가 ...

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