Skip to content

strconv

Resmi dokümantasyon: strconv package - strconv - Go Packages

strconv paketi temel veri tipleri ile string gösterimleri arasında dönüşüm işlemlerini implement eder.

İçe Aktarma

go
import (
  "strconv"
)

Aşağıda örneklerle nasıl kullanılacağı gösterilecektir.

String'den Tamsayıya Dönüştürme

go
func Atoi(s string) (int, error)
  • s - Dönüştürülecek string
go
func TestAoti(t *testing.T) {
  ints, err := strconv.Atoi("456789")
  fmt.Println(ints, err)
}
=== RUN   TestAoti
456789 <nil>
--- PASS: TestAoti (0.00s)
PASS

Tamsayıdan String'e Dönüştürme

go
func Itoa(i int) string
  • i - Dönüştürülecek tamsayı
go
func TestIota(t *testing.T) {
   str := strconv.Itoa(114)
   fmt.Println(str)
}
=== RUN   TestIota
114
--- PASS: TestIota (0.00s)
PASS

String'den Boolean'a Dönüştürme

go
func ParseBool(str string) (bool, error)
  • s - Dönüştürülecek string

Dönüştürülebilen string'ler aşağıdaki gibidir

"1", "t", "T", "true", "TRUE", "True" // true
"0", "f", "F", "false", "FALSE", "False" // false
go
func TestAtob(t *testing.T) {
   parseBool, err := strconv.ParseBool("1")
   fmt.Println(parseBool, err)

   b, err := strconv.ParseBool("true")
   fmt.Println(b, err)

   b2, err := strconv.ParseBool("FALSE")
   fmt.Println(b2, err)
}
=== RUN   TestAotb
true <nil>
true <nil>
false <nil>
--- PASS: TestAotb (0.00s)
PASS

Boolean'dan String'e Dönüştürme

go
func FormatBool(b bool) string
  • b - Boolean değer
go
func TestBota(t *testing.T) {
   fmt.Println(strconv.FormatBool(true))
   fmt.Println(strconv.FormatBool(false))
}
=== RUN   TestBota
true
false
--- PASS: TestBota (0.00s)
PASS

Go String'ine Dönüştürme

Her ikisi de string'i tırnak işaretli Go string'ine dönüştürür, farkı ikincisi ASCII olmayan karakterleri \u ile escape eder.

go
func TestQuote(t *testing.T) {
  fmt.Println(strconv.Quote("hello 世界"))
  fmt.Println(strconv.QuoteToASCII("hello 世界"))
}
=== RUN   TestQuote
"hello 世界"
"hello \u4e16\u754c"
--- PASS: TestQuote (0.00s)
PASS

String'den Kayan Noktaya Dönüştürme

go
func ParseFloat(s string, bitSize int) (float64, error)
  • s - Dönüştürülecek string
  • bitSize - Bit boyutu
go
func TestParseFloat(t *testing.T) {
   float, err := strconv.ParseFloat("1.145114", 64)
   fmt.Println(float, err)

   float, err = strconv.ParseFloat("2.3333333333333333333", 64)
   fmt.Println(float, err)
}
=== RUN   TestFloat
1.145114 <nil>
2.3333333333333335 <nil>
--- PASS: TestFloat (0.00s)
PASS

Kayan Noktadan String'e Dönüştürme

String kayan noktaya dönüştürülürken, resmi dokümantasyon farklı stiller çıktı almak için birkaç format metodu sunar.

// 'b' (-ddddp±ddd, binary üs),
// 'e' (-d.dddde±dd, küçük e onluk üs),
// 'E' (-d.ddddE±dd, büyük E onluk üs),
// 'f' (-ddd.dddd, üs yok), // Özel gereksinim yoksa genellikle bu kullanılır
// 'g' (Büyük üsler için 'e' formatı, küçük üsler için 'f' formatı),
// 'G' (Büyük üsler için 'E' formatı, küçük üsler için 'f' formatı),
// 'x' (-0xd.ddddp±ddd, ondalık kesir ve binary üs),
// 'X' (-0Xd.ddddP±ddd, ondalık kesir ve binary üs).

Dönüştürme fonksiyonu

go
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
  • f - Dönüştürülecek kayan nokta
  • fmt - Format tipi
  • prec - Hassasiyet, g/G durumu hariç maksimum anlamlı basamak sayısını gösterir, diğer durumlarda ondalık basamakları kaç basamağa yuvarlayacağını gösterir
  • bitSize - Bit boyutu

Elbette genellikle en çok 'f' kullanılarak ondalık format dönüştürmesi yapılır.

go
func TestFormatFloat(t *testing.T) {
   f := 1315643.14159261234567891011
   fmt.Println(strconv.FormatFloat(f, 'f', 6, 64))
   fmt.Println(strconv.FormatFloat(f, 'b', 6, 64))
   fmt.Println(strconv.FormatFloat(f, 'e', 6, 64))
   fmt.Println(strconv.FormatFloat(f, 'x', 6, 64))
   fmt.Println(strconv.FormatFloat(f, 'g', 6, 64))
   fmt.Println(strconv.FormatFloat(1.111, 'g', 6, 64))
}
=== RUN   TestFormatFloat
1315643.141593
5650644266346967p-32
1.315643e+06
0x1.4133b2p+20
1.31564e+06
1.111
--- PASS: TestFormatFloat (0.00s)
PASS

String'den Karmaşık Sayıya Dönüştürme

go
func ParseComplex(s string, bitSize int) (complex128, error)
  • s - Dönüştürülecek string
  • bitSize - Bit boyutu
go
func TestParseComplex(t *testing.T) {
   fmt.Println(strconv.ParseComplex("1+2i", 128))
   fmt.Println(strconv.ParseComplex("1+2j", 128))
}
=== RUN   TestParseComplex
(1+2i) <nil>
(0+0i) strconv.ParseComplex: parsing "1+2j": invalid syntax
--- PASS: TestParseComplex (0.00s)
PASS

Karmaşık Sayıdan String'e Dönüştürme

go
func FormatComplex(c complex128, fmt byte, prec, bitSize int) string

c - Karmaşık sayı

fmt - Format tipi, kayan nokta format tipine bakın

prec - Kayan nokta hassasiyetine bakın

bitSize - Bit boyutu

go
func TestFormatComplex(t *testing.T) {
   fmt.Println(strconv.FormatComplex(complex(1.1, 12), 'f', 2, 128))
   fmt.Println(strconv.FormatComplex(complex(5.6, 2.8), 'b', 2, 128))
   fmt.Println(strconv.FormatComplex(complex(18.88999, 89.7), 'g', 2, 128))
}
=== RUN   TestFormatComplex
(1.10+12.00i)
(6305039478318694p-50+6305039478318694p-51i)
(19+90i)
--- PASS: TestFormatComplex (0.00s)
PASS

String'e Veri Ekleme

Java gibi diğer dillerde "1"+1 sonucu "11" olur, Java otomatik olarak tip dönüşümü yapar, ancak Go dilinde bu tür bir işlem izin verilmez çünkü her iki veri tipi de farklıdır. Bu nedenle strconv altındaki Append fonksiyonlarını kullanmanız gerekir, spesifik parametreler yukarıdaki veri dönüşüm fonksiyonlarıyla aynıdır.

go
func TestAppend(t *testing.T) {
   bytes := []byte("Burada bazı veriler var:")
   bytes = strconv.AppendInt(bytes, 10, 10)
   bytes = strconv.AppendFloat(bytes, 1.2222, 'f', 2, 64)
   bytes = strconv.AppendBool(bytes, false)
   fmt.Println(string(bytes))
}
=== RUN   TestAppend
Burada bazı veriler var:101.22false
--- PASS: TestAppend (0.00s)
PASS

Golang by www.golangdev.cn edit