Skip to content

Latest commit

ย 

History

History
63 lines (50 loc) ยท 2.29 KB

type.md

File metadata and controls

63 lines (50 loc) ยท 2.29 KB

type ๋ณ€ํ™˜

ํ˜•๋ณ€ํ™˜ ๊ด€๋ จ๋œ ๋‚ด์šฉ ์ •๋ฆฌโœ

โœ”๏ธ[]byte โ†’ big.Int

result := new(big.Int) // big.Intํ˜• ๋ณ€์ˆ˜ ์„ ์–ธ
result.SetBytes(before_data) // []byte ํƒ€์ž…์ธ before_data ๊ฐ’์„ big.Int๋กœ ๋ณ€ํ™˜

โœ”๏ธbig.Int โ†’ []byte

convert_bytes := result.Bytes() //big.Int์ธ result์— ๋Œ€ํ•ด Bytes๋กœ ๋ณ€ํ™˜ (๋‚ด์žฌ๋œ ํ•จ์ˆ˜ ์กด์žฌํ•จ)

โœ”๏ธString โ†’ big.Int

result := new(big.Int) // big.Intํ˜• ๋ณ€์ˆ˜ ์„ ์–ธ
result.SetString(before_str) // string ํƒ€์ž…์ธ before_str ๊ฐ’์„ big.Int๋กœ ๋ณ€ํ™˜

โœ”๏ธbig.Int โ†’ String

convert_str := result.String() //big.Int์ธ result์— ๋Œ€ํ•ด String์œผ๋กœ ๋ณ€ํ™˜ (๋‚ด์žฌ๋œ ํ•จ์ˆ˜ ์กด์žฌํ•จ)

โœ”๏ธstring โ†’ Int

str2int := strcov.Atoi(Age) //string์ธ ๊ฐ’ int๋กœ ๋ณ€ํ™˜

โœ”๏ธstring โ†’ Int

int2str := strcov.Itoa(Age) //int์ธ ๊ฐ’ strnig์œผ๋กœ ๋ณ€ํ™˜

โœ”๏ธType Assertion VS Type Conversion

  • Type Assertion

    • ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋ผ๊ธฐ ๋ณด๋‹จ, ํ•ด๋‹นํ•˜๋Š” ํƒ€์ž…์˜ ๊ฐ’์„ ํ™•์ธํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•จ
    • ๋‹ค์‹œ๋งํ•ด, ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๊ฐ–๊ณ ์žˆ๋Š” ์‹ค์ œ ๊ฐ’์„ ํ™•์ธํ•˜๋Š” ๊ฒƒ
  • Type Conversion

    • ๋ง๊ทธ๋Œ€๋กœ, ํƒ€์ž…์„ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” casting๊ณผ ๊ฐ™์€ ์˜๋ฏธ์ž„
    • go์—์„œ๋Š” ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ•ด์ฃผ๋ ค๋ฉด, ๋ช…์‹œ์ ์œผ๋กœ ํ˜•๋ณ€ํ™˜์„ ํ•ด์ค˜์•ผ ํ•จ
    var tall float64 = 167.5
    var tall_upper uint = uint(tall)
    

โœ”๏ธ Explicit type conversion VS Implicit type conversion

  • Java์—์„œ๋Š” ๋ช…์‹œ์  ํƒ€์ž… ๋ณ€ํ™˜(Explicit type conversion), ์•”์‹œ์  ํƒ€์ž… ๋ณ€ํ™˜(Implicit type conversion) ๋‘˜ ๋‹ค ์ง€์›
  • ๋ฐ˜๋ฉด Go์—์„œ๋Š” ๋ช…์‹œ์  ํƒ€์ž… ๋ณ€ํ™˜๋งŒ ์ง€์›ํ•ด์คŒ
    • ๋ช…์‹œ์  ํƒ€์ž… ๋ณ€ํ™˜ : ์ง์ ‘ ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ณ€๊ฒฝ
    • ์•”์‹œ์  ํƒ€์ž… ๋ณ€ํ™˜(๋˜๋Š” ๋ฌต์‹œ์  ํƒ€์ž… ๋ณ€ํ™˜) : ์ปดํŒŒ์ผ๋Ÿฌ์— ์˜ํ•ด ์ž๋™์œผ๋กœ ํƒ€์ž… ๋ณ€ํ™˜์ด ์ด๋ค„์ง€๋Š” ๊ฒƒ
      • C, Java์—์„œ์ฒ˜๋Ÿผ int ๋ฐ์ดํ„ฐ์™€ float ๋ฐ์ดํ„ฐ๋ฅผ ๋ง์…ˆํ•œ๋‹ค๋ฉด, ์ž๋™์œผ๋กœ float๋กœ ํƒ€์ž…์ด ๋ณ€ํ™˜๋˜๋Š” ๊ฒฝ์šฐ
      • ํ‘œํ˜„๋ฒ”์œ„๊ฐ€ ์ข์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์—์„œ ๋„“์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ์˜ ๋ณ€ํ™˜๋งŒ ํ—ˆ์šฉ๋จ. (int๋Š” double๋กœ ํ˜•๋ณ€ํ™˜ ๊ฐ€๋Šฅ, double์€ int๋กœ ๋ถˆ๊ฐ€๋Šฅ)

... ing

์ฐธ๊ณ