잠시만 기다려 주세요

     '왜 이재명은 자꾸만 국민들 보고 길바닥에 나가라고 하는 건가.. 정작 당신들은 뜨뜻한 곳에서 입만 나불거리고 있으면서...'
전체검색 :  
이번주 로또 및 연금번호 발생!!   |  HOME   |  여기는?   |  바다물때표   |  알림 (16)  |  여러가지 팁 (1054)  |  추천 및 재미 (150)  |  자료실 (22)  |  
시사, 이슈, 칼럼, 평론, 비평 (584)  |  끄적거림 (127)  |  문예 창작 (703)  |  바람 따라 (69)  |  시나리오 (760)  |  드라마 대본 (248)  |  
살인!


    전 체

golang - Golang 문자열 변환 방법(문자열, 숫자, 등 data 파싱방법) 타입 변환, 형 변환...
이 름 : 바다아이   |   조회수 : 1057         짧은 주소 : https://www.bada-ie.com/su/?CgwoY_YCTLnR

안녕하세요.

 

오늘은 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
}


결과 :

say hi
a:  100
type a:  string
aa:  100
type aa:  string
b:  100
type b:  int
bb:  100
type bb:  int64
c:  true
type c:  string
d:  1.3
type d:  string
f:  11
type f:  int32
g:  11
type g:  int64


모두 즐거운 코딩하세요~

 

출처 : https://ithub.tistory.com/331

 
| |





      6 page / 125 page
번 호 카테고리 제 목 이름 조회수
3583 정치 .. . ..... . 바다아이 715
3582 사회 .. . , ... ... 바다아이 701
3581 정치 ... ?? ... ... ... ... !! !!. .. . 바다아이 943
3580 정치 .. .. ..... .. ... .. -.-; 바다아이 698
3579 정치 .... ... 2 2 .. .... .. ....!! !!... !!. 바다아이 709
3578 스포츠 .. .. .. ... .. .. !! 바다아이 1125
3577 정치 ... ... ... 바다아이 1023
3576 컴퓨터/핸드폰 . .... 바다아이 1189
3575 정치 . .... -.-;... 바다아이 745
3574 옹알이, 잡담 .. ..... -.-; 바다아이 1239
3573 날림 시 ... 바다아이 862
3572 날림 시 ... 바다아이 807
현재글 golang Golang (, , data ) , ... 바다아이 1058
3570 golang golang sort ... 바다아이 1228
3569 일화, 일상 .. .. -.-; ... .. 바다아이 1125
3568 정치 .. .. .. . 바다아이 742
3567 날림 시 2... 바다아이 1183
3566 날림 시 ... 바다아이 788
3565 날림 시 2.. 바다아이 876
3564 사회 ... ...?? ' ?? ??' . 바다아이 986
3563 사회 ... , ... ..... 바다아이 928
3562 생활/쇼핑 .. .... .... ??... ... -.-; 바다아이 1175
3561 날림 시 ... .... 바다아이 741
3560 날림 시 2... 바다아이 783
3559 문화 2023 3 1... 3.1 .. ... .... . 바다아이 717
3558 생각의 날개 ... 바다아이 1079
3557 옹알이, 잡담 바다아이 999
3556 스포츠 ... .. 40 .. ... ?? .. !! -.-; 바다아이 1260
3555 날림 시 바다아이 1100
3554 문화 ... .. 바다아이 696
| |









Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천  실행시간 : 0.0561
to webmaster... gogo sea. gogo sea.