| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- 디자인패턴
- go
- GIT
- context7
- typescript
- database/sql
- golang
- elasticsearch
- Intellij
- 티스토리챌린지
- MSA
- 구조체
- GoF
- AI
- 관측 가능성
- Infra
- Kubernetes
- RDS
- esbuild
- replication lag
- AWS
- javascript
- go-sql-driver
- blank import
- 통합 로깅 시스템
- goland
- sqs fifo queue
- 오블완
- logging
- 캡슐화
Archives
- Today
- Total
Fall in IT.
Golang 문자열 변환 방법(문자열, 숫자, 등 data 파싱방법) 본문
반응형
안녕하세요.
오늘은 Golang에서 숫자(int)인 문자열을 숫자(int)로 변경하기 등 자주 사용되는 data parsing 방법에 대해서 알아보겠습니다.
package main
import (
"fmt"
"reflect"
"strconv"
)
func main() {
fmt.Println("say hi")
// 1. int to string - 숫자(정수)를 문자열로 변환
a := strconv.Itoa(100)
fmt.Println("a: ", a) // a: 100
fmt.Println("type a: ", reflect.TypeOf(a)) // type a: string
// 1-1. int to string - 100을 10진수 문자열로 변환
aa := strconv.FormatInt(100, 10)
fmt.Println("aa: ", aa) // aa: 100
fmt.Println("type aa: ", reflect.TypeOf(aa)) // type aa: string
// 2. string to int - 문자열을 숫자(정수) 변환
b, _ := strconv.Atoi("100")
fmt.Println("b: ", b) // b: 100
fmt.Println("type b: ", reflect.TypeOf(b)) // type b: int
bb, _ := strconv.ParseInt("100", 10, 64)
fmt.Println("bb: ", bb) // bb: 100
fmt.Println("type bb: ", reflect.TypeOf(bb)) // type bb: int64
// 3. bool to string - 불을 문자열로 변환
c := strconv.FormatBool(true)
fmt.Println("c: ", c) // c: true
fmt.Println("type c: ", reflect.TypeOf(c)) // type c: string
// 4. flot to string - 숫자(실수)를 문자열로 변환
d := strconv.FormatFloat(1.3, 'f', -1, 32)
fmt.Println("d: ", d) // d: 1.3
fmt.Println("type d: ", reflect.TypeOf(d)) //type d: string
// 5. int -> int32, int32 -> int64
var e int = 11
f := int32(e)
fmt.Println("f: ", f) // f: 11
fmt.Println("type f: ", reflect.TypeOf(f)) // type f: int32
g := int64(f)
fmt.Println("g: ", g) // g: 11
fmt.Println("type g: ", reflect.TypeOf(g)) // type g: int64
}
모두 즐거운 코딩하세요~
반응형
'프로그래밍언어 > Golang' 카테고리의 다른 글
| Go 1.18버전부터 추가 된 generic 사용해보기 (0) | 2022.05.02 |
|---|---|
| Golang Slice 중복 제거하는 방법 (0) | 2022.03.06 |
| Go 함수에서 포인터형 매개변수 사용하기 (0) | 2021.06.27 |
| Golang JSON 인코딩 / 디코딩 방법 (1) | 2020.09.28 |
| Go Time 간단하게 변환하는방법 (0) | 2020.07.30 |
Comments