Go 기초

Go Strings and the strings Package

Join, search, split, trim and slice text in Go.

  • 초급
  • 2분 분량
  • 실행 가능한 예제 1개
배울 내용
  • Building strings
  • Contains, Index, Split
  • Slicing a string
message[0:5]
H0
e1
l2
l3
o4
,5
The end index 5 is not included.

Read-only text with many helpers

A Go string is a read-only sequence of bytes, usually UTF-8 text. You cannot change a string in place, but you can build new ones, and the strings package has functions for most everyday jobs.

+= builds a new string. len counts bytes. Contains and Index search the text, Split cuts it at a separator and returns a slice, and message[0:5] takes the bytes from position 0 up to, but not including, 5.

핵심 포인트

  1. A string cannot be changed in place.
  2. len counts bytes.
  3. message[0:5] takes bytes 0 to 4.

실행 가능한 Go 예제

Go
코드 실행 →
main.go
package main

import (
	"fmt"
	"strings"
)

func main() {
	message := "Hello, Go"
	message += "!"

	fmt.Println(message)
	fmt.Println(len(message), strings.ToUpper(message))
	fmt.Println(strings.Contains(message, "Go"), strings.Index(message, "Go"))
	fmt.Println(strings.Split("a,b,c", ","))
	fmt.Println(strings.Repeat("=", 5), strings.TrimSpace("  padded  "))
	fmt.Println(message[0:5], string(message[7]))
}
이 예제의 실행 버튼 만들기 코드 실행 결과
Hello, Go!
10 HELLO, GO!
true 7
[a b c]
===== padded
Hello G

연습

예제를 온라인 컴파일러에서 열고 값을 바꾸거나 한 줄을 추가한 다음, 실행하기 전에 출력을 예상해 보세요. 그런 다음 일부러 코드를 망가뜨리고 컴파일러 메시지를 읽어 보세요.

이 레슨을 마쳤나요?

학습 완료로 표시하면 진행 상황을 기록할 수 있습니다.

다음: Reverse a String →

챌린지로 연습하기

이 레슨에서 배운 내용으로 실제 문제를 풀어 보세요. 모든 챌린지는 브라우저에서 실행되며 즉시 테스트와 AI 리뷰를 받을 수 있습니다.

모든 챌린지