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