gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

README.md (3050B)


      1 # cast
      2 
      3 [![Build Status](https://github.com/spf13/cast/actions/workflows/ci.yml/badge.svg)](https://github.com/spf13/cast/actions/workflows/ci.yml)
      4 [![PkgGoDev](https://pkg.go.dev/badge/mod/github.com/spf13/cast)](https://pkg.go.dev/mod/github.com/spf13/cast)
      5 ![Go Version](https://img.shields.io/badge/go%20version-%3E=1.16-61CFDD.svg?style=flat-square)
      6 [![Go Report Card](https://goreportcard.com/badge/github.com/spf13/cast)](https://goreportcard.com/report/github.com/spf13/cast)
      7 
      8 Easy and safe casting from one type to another in Go
      9 
     10 Don’t Panic! ... Cast
     11 
     12 ## What is Cast?
     13 
     14 Cast is a library to convert between different go types in a consistent and easy way.
     15 
     16 Cast provides simple functions to easily convert a number to a string, an
     17 interface into a bool, etc. Cast does this intelligently when an obvious
     18 conversion is possible. It doesn’t make any attempts to guess what you meant,
     19 for example you can only convert a string to an int when it is a string
     20 representation of an int such as “8”. Cast was developed for use in
     21 [Hugo](https://gohugo.io), a website engine which uses YAML, TOML or JSON
     22 for meta data.
     23 
     24 ## Why use Cast?
     25 
     26 When working with dynamic data in Go you often need to cast or convert the data
     27 from one type into another. Cast goes beyond just using type assertion (though
     28 it uses that when possible) to provide a very straightforward and convenient
     29 library.
     30 
     31 If you are working with interfaces to handle things like dynamic content
     32 you’ll need an easy way to convert an interface into a given type. This
     33 is the library for you.
     34 
     35 If you are taking in data from YAML, TOML or JSON or other formats which lack
     36 full types, then Cast is the library for you.
     37 
     38 ## Usage
     39 
     40 Cast provides a handful of To_____ methods. These methods will always return
     41 the desired type. **If input is provided that will not convert to that type, the
     42 0 or nil value for that type will be returned**.
     43 
     44 Cast also provides identical methods To_____E. These return the same result as
     45 the To_____ methods, plus an additional error which tells you if it successfully
     46 converted. Using these methods you can tell the difference between when the
     47 input matched the zero value or when the conversion failed and the zero value
     48 was returned.
     49 
     50 The following examples are merely a sample of what is available. Please review
     51 the code for a complete set.
     52 
     53 ### Example ‘ToString’:
     54 
     55     cast.ToString("mayonegg")         // "mayonegg"
     56     cast.ToString(8)                  // "8"
     57     cast.ToString(8.31)               // "8.31"
     58     cast.ToString([]byte("one time")) // "one time"
     59     cast.ToString(nil)                // ""
     60 
     61 	var foo interface{} = "one more time"
     62     cast.ToString(foo)                // "one more time"
     63 
     64 
     65 ### Example ‘ToInt’:
     66 
     67     cast.ToInt(8)                  // 8
     68     cast.ToInt(8.31)               // 8
     69     cast.ToInt("8")                // 8
     70     cast.ToInt(true)               // 1
     71     cast.ToInt(false)              // 0
     72 
     73 	var eight interface{} = 8
     74     cast.ToInt(eight)              // 8
     75     cast.ToInt(nil)                // 0