• Stars
    star
    3,073
  • Rank 14,651 (Top 0.3 %)
  • Language
    Go
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

safe and easy casting from one type to another in Go

cast

GitHub Workflow Status PkgGoDev Go Version Go Report Card

Easy and safe casting from one type to another in Go

Don’t Panic! ... Cast

What is Cast?

Cast is a library to convert between different go types in a consistent and easy way.

Cast provides simple functions to easily convert a number to a string, an interface into a bool, etc. Cast does this intelligently when an obvious conversion is possible. It doesn’t make any attempts to guess what you meant, for example you can only convert a string to an int when it is a string representation of an int such as “8”. Cast was developed for use in Hugo, a website engine which uses YAML, TOML or JSON for meta data.

Why use Cast?

When working with dynamic data in Go you often need to cast or convert the data from one type into another. Cast goes beyond just using type assertion (though it uses that when possible) to provide a very straightforward and convenient library.

If you are working with interfaces to handle things like dynamic content you’ll need an easy way to convert an interface into a given type. This is the library for you.

If you are taking in data from YAML, TOML or JSON or other formats which lack full types, then Cast is the library for you.

Usage

Cast provides a handful of To_____ methods. These methods will always return the desired type. If input is provided that will not convert to that type, the 0 or nil value for that type will be returned.

Cast also provides identical methods To_____E. These return the same result as the To_____ methods, plus an additional error which tells you if it successfully converted. Using these methods you can tell the difference between when the input matched the zero value or when the conversion failed and the zero value was returned.

The following examples are merely a sample of what is available. Please review the code for a complete set.

Example ‘ToString’:

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"

Example ‘ToInt’:

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

More Repositories

1

cobra

A Commander for modern Go CLI interactions
Go
37,720
star
2

viper

Go configuration with fangs
Go
27,043
star
3

spf13-vim

The ultimate vim distribution
Vim Script
15,558
star
4

afero

A FileSystem Abstraction System for Go
Go
5,913
star
5

cobra-cli

Cobra CLI tool to generate applications and commands
Go
695
star
6

hyde

Port of Mdo's excellent theme to Hugo
CSS
553
star
7

nitro

Quick and easy performance analyzer library for golang
Go
300
star
8

jwalterweatherman

So you always leave a note
Go
261
star
9

PIV

PHP Integration environment for Vim
Vim Script
251
star
10

spf13.com

Hugo Based website of Steve Francia
CSS
143
star
11

dagobah

dagobah is an awesome RSS feed aggregator & reader written in Go inspired by planet
Go
136
star
12

kaiju

Open source discussion server
JavaScript
113
star
13

herring-cove

Port of arnp's excellent theme to Hugo
CSS
43
star
14

vim-colors

Collection of color schemes for VIM
Vim Script
40
star
15

mongoProducts

Sample data for Mongo Product Presentation
37
star
16

tri

Todo CLI app
Go
32
star
17

dotfiles

spf13's dotfiles
Shell
27
star
18

firstGoApp-Planet

Built for OSCON2014 .. Each step is in it's own branch
Go
20
star
19

Vogon

A MongoDB PHP based blog
JavaScript
16
star
20

Milieu

Location based check in application powered by MongoDB written in Ruby
Ruby
13
star
21

cobra.dev

Cobra website
HTML
8
star
22

relativeTime

Golang relative date formatting (1 day ago, in 2 weeks), etc.
7
star
23

stevefrancia.com

Homepage Website for SteveFrancia.com
HTML
6
star
24

wp_GitHub_Code_Viewer

Plugin for Wordpress to insert file from github into a blog post
PHP
5
star
25

spf13

5
star
26

spf13.github.com

Git Hub Page for spf13
4
star
27

vim-gocode

Vim Plugin of nsf/gocode for use with Vundle/Pathogen
Vim Script
3
star
28

vimbrant

Harnessing the power of vim to turn source into highlighted html
3
star
29

One-More-For-The-Road

A Skeleton App for Sinatra using MongoDB
JavaScript
3
star
30

instant

Instant theme for Hugo (derived from Black Tie Instant)
HTML
2
star
31

Autostamp

Auto update the time and version stamp in the header for VIM
Vim Script
1
star
32

YourFirstRubyMongoDBApp

Skeleton for your first ruby MongoDB app to go along with my workshop
Ruby
1
star