php editor Xiaoxin introduces you how to check whether a variable is a string, integer or floating point number. In PHP, we can use some built-in functions to achieve this purpose. First, use the is_string() function to check whether a variable is of string type. If true is returned, it means that the variable is of type string. Next, use the is_int() function to check whether a variable is of integer type. Likewise, if true is returned, it means that the variable is of integer type. Finally, use the is_float() function to check whether a variable is of floating point type. Likewise, if true is returned, it means that the variable is of floating point type. By using these functions, we can easily check the type of a variable and perform appropriate operations as needed.
Question content
Get this type map[string]interface{}
because I want to allow for both strings
and integers
map
But how do I convert a map to map[string]string
and return an error if any value is "not supported"?
value, ok := v.(string)
If the value is an integer, an error will be thrown
Solution
Go supports type switches:
For your use case it would look like:
m := map[string]string{} for k,v := range values { switch value := v.(type) { switch value := v.(type) { case int: m[k] = fmt.Sprintf("%v", value) case float64: m[k] = fmt.Sprintf("%v", value) case string: m[k] = fmt.Sprintf("%v", value) default: return nil,fmt.Errorf("unknown type %T", v) } } return m,nil
If you want, you can merge these cases into one case, for example https://www.php.cn/link/658953f1f681915f543a40eef9acb562
The above is the detailed content of Check if variable is string/int/float?. For more information, please follow other related articles on the PHP Chinese website!

Article discusses iterating through maps in Go, focusing on safe practices, modifying entries, and performance considerations for large maps.Main issue: Ensuring safe and efficient map iteration in Go, especially in concurrent environments and with l

The article discusses creating and manipulating maps in Go, including initialization methods and adding/updating elements.

The article discusses differences between arrays and slices in Go, focusing on size, memory allocation, function passing, and usage scenarios. Arrays are fixed-size, stack-allocated, while slices are dynamic, often heap-allocated, and more flexible.

The article discusses creating and initializing slices in Go, including using literals, the make function, and slicing existing arrays or slices. It also covers slice syntax and determining slice length and capacity.

The article explains how to create and initialize arrays in Go, discusses the differences between arrays and slices, and addresses the maximum size limit for arrays. Arrays vs. slices: fixed vs. dynamic, value vs. reference types.

Article discusses syntax and initialization of structs in Go, including field naming rules and struct embedding. Main issue: how to effectively use structs in Go programming.(Characters: 159)

The article explains creating and using pointers in Go, discussing benefits like efficient memory use and safe management practices. Main issue: safe pointer use.

The article discusses the benefits of using Go (Golang) in software development, focusing on its concurrency support, fast compilation, simplicity, and scalability advantages. Key industries benefiting include technology, finance, and gaming.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
