Go 기초

Go Data Types: int, float64, bool, rune, string

The basic types of Go, how to print them with Printf, and why conversions are always written out.

  • 초급
  • 2분 분량
  • 실행 가능한 예제 1개
배울 내용
  • int, float64, bool, string
  • rune
  • Explicit conversions
Value → type
30int
19.99float64
truebool
'G'rune (int32)

One type per variable

Go is statically typed: every variable has one type that never changes. The types you will use most are int, float64, bool, string and rune, which is one Unicode character.

Go never converts numbers for you, so float64(age) is needed before adding a float. Printf formats values with verbs: %d for integers, %.2f for two decimals, %t for booleans, %c for a character and %T for the type. A rune is another name for int32.

핵심 포인트

  1. Go never converts numbers for you: write float64(age).
  2. A rune is one Unicode character.
  3. %T prints the type of a value.

실행 가능한 Go 예제

Go
코드 실행 →
main.go
package main

import "fmt"

func main() {
	var age int = 30
	var price float64 = 19.99
	var ready bool = true
	var initial rune = 'G'
	var name string = "Gopher"

	total := float64(age) + price // conversions are always explicit
	fmt.Printf("%d %.2f %t %c %s\n", age, price, ready, initial, name)
	fmt.Printf("total = %.2f\n", total)
	fmt.Printf("%T %T %T\n", age, price, initial)
}
이 예제의 실행 버튼 만들기 코드 실행 결과
30 19.99 true G Gopher
total = 49.99
int float64 int32

연습

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

이 레슨을 마쳤나요?

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

다음: Operators and Arithmetic →

챌린지로 연습하기

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

모든 챌린지