Menu
Coddy logo textTech

패키지와 임포트

Coddy GO 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 107개 중 3번째.

모든 Go 파일은 package 선언으로 시작합니다. import 키워드는 Go 표준 라이브러리의 패키지를 가져와 해당 패키지의 함수를 사용할 수 있게 합니다.

단일 패키지 가져오기

import "fmt"

import 블록을 사용하여 여러 패키지 가져오기

import (
    "fmt"
    "math"
    "strings"
)

가져온 패키지의 함수는 패키지 이름을 접두사로 사용하세요

package main

import (
    "fmt"
    "strings"
    "math"
)

func main() {
    upper := strings.ToUpper("hello")
    fmt.Println(upper) // 출력: HELLO

    root := math.Sqrt(16)
    fmt.Println(root) // 출력: 4
}

일반적인 표준 라이브러리 패키지

"fmt"     // 형식화된 I/O (Print, Scan, Sprintf)
"strings" // 문자열 조작 (ToUpper, Contains, Replace)
"math"    // 수학 함수 (Sqrt, Pow, Pi)
"strconv" // 문자열 변환 (Itoa, Atoi)

packagename.FunctionName을 사용하여 package에서 함수를 가져옵니다. Go에서는 실제로 사용하는 package만 import할 수 있습니다. 사용하지 않은 import는 컴파일 오류를 발생시킵니다.

challenge icon

챌린지

중급

utils.go의 import 문을 수정하여 functions가 올바르게 작동하도록 하세요. stringsmath 패키지를 import해야 합니다:

  • FormatTextstrings.ToUpper를 사용합니다
  • ContainsWordstrings.Contains를 사용합니다
  • SquareRootmath.Sqrt를 사용합니다

직접 해보기

package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
)

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    line := scanner.Text()

    parts := strings.Fields(line)
    numStr := parts[len(parts)-1]
    text := strings.Join(parts[:len(parts)-1], " ")
    num, _ := strconv.ParseFloat(numStr, 64)

    fmt.Printf("Upper: %s\n", FormatText(text))
    fmt.Printf("Contains 'world': %t\n", ContainsWord(text, "world"))
    fmt.Printf("Sqrt: %.2f\n", SquareRoot(num))
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

객체 지향 프로그래밍의 모든 레슨

직접 연습해 보세요: 온라인 Go 컴파일러