Go에서 부울을 문자열로 변환

이 기사에서는 Go에서 부울을 문자열 데이터 유형으로 변환하는 방법을 소개합니다.
FormatBool
을 사용하여 Go에서 부울을 문자열로 변환
아래 예에서 FormatBool
은 a
값에 따라 true 또는 false를 반환합니다.
package main
import (
"fmt"
"strconv"
)
func main() {
a := true
str := strconv.FormatBool(a)
fmt.Println(str)
}
출력:
true
다음 예에서 FormatBool
은 부울 값을 인수로 받아 문자열로 변환합니다.
package main
import (
"fmt"
"strconv"
)
func main() {
boolVal := true
strVal := strconv.FormatBool(boolVal)
fmt.Printf("Type of strVal: %T\n", strVal)
fmt.Printf("Type of boolVal: %T\n", boolVal)
fmt.Println()
fmt.Printf("Value of strVal: %v\n", strVal)
fmt.Printf("Value of boolVal: %v", boolVal)
}
출력:
Type of strVal: string
Type of boolVal: bool
Value of strVal: true
Value of boolVal: true
Sprintf
를 사용하여 Go에서 Boolean을 문자열로 변환
Sprintf
함수를 사용하여 부울을 문자열로 변환할 수도 있습니다.
package main
import (
"fmt"
)
func main() {
boolVal := false
strVal := fmt.Sprintf("%v", boolVal)
fmt.Printf("Type of strVal: %T\n", strVal)
fmt.Printf("Type of boolVal: %T\n", boolVal)
fmt.Println()
fmt.Printf("Value of strVal: %v\n", strVal)
fmt.Printf("Value of boolVal: %v", boolVal)
}
출력:
Type of strVal: string
Type of boolVal: bool
Value of strVal: false
Value of boolVal: false
튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다