Skip to content

spf13/cast

ที่เก็บข้อมูล: spf13/cast: safe and easy casting from one type to another in Go (github.com)

เอกสาร: spf13/cast: safe and easy casting from one type to another in Go (github.com)

บทนำ

cast เป็นไลบรารีแปลงประเภทอย่างง่าย ใช้สำหรับการแปลงประเภทหนึ่งไปยังอีกประเภทหนึ่งอย่างรวดเร็ว ช่วยประหยัดการดำเนินการที่ยุ่งยากมากมาย

การติดตั้ง

go get https://github.com/spf13/cast

ตัวอย่าง

สตริง

go
cast.ToString("mayonegg")         // "mayonegg"
cast.ToString(8)                  // "8"
cast.ToString(8.31)               // "8.31"
cast.ToString([]byte("one time")) // "one time"
cast.ToString(nil)                // ""

var foo interface{} = "one more time"
cast.ToString(foo)                // "one more time"

จำนวนเต็ม

go
cast.ToInt(8)                  // 8
cast.ToInt(8.31)               // 8
cast.ToInt("8")                // 8
cast.ToInt(true)               // 1
cast.ToInt(false)              // 0

var eight interface{} = 8
cast.ToInt(eight)              // 8
cast.ToInt(nil)                // 0

Golang by www.golangdev.cn edit