Go 기초
Go Strings and the strings Package
Join, search, split, trim and slice text in Go.
배울 내용
- Building strings
- Contains, Index, Split
- Slicing a string
message[0:5]
H0
e1
l2
l3
o4
,5
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.
핵심 포인트
- A string cannot be changed in place.
- len counts bytes.
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연습
예제를 온라인 컴파일러에서 열고 값을 바꾸거나 한 줄을 추가한 다음, 실행하기 전에 출력을 예상해 보세요. 그런 다음 일부러 코드를 망가뜨리고 컴파일러 메시지를 읽어 보세요.
이 레슨을 마쳤나요?
학습 완료로 표시하면 진행 상황을 기록할 수 있습니다.
진행 상황이 이 브라우저에 저장되어 있습니다 로그인하면 모든 기기에서 이어갈 수 있습니다.
챌린지로 연습하기
이 레슨에서 배운 내용으로 실제 문제를 풀어 보세요. 모든 챌린지는 브라우저에서 실행되며 즉시 테스트와 AI 리뷰를 받을 수 있습니다.
- 모음 개수 세기 주어진 텍스트에서 모음의 개수를 세세요. 쉬움
- Initials From a Name Build uppercase initials from a full name. 쉬움