search
HomeBackend DevelopmentGolangCheck if variable is string/int/float?

检查变量是否为 string/int/float?

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!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete
How do you iterate through a map in Go?How do you iterate through a map in Go?Apr 28, 2025 pm 05:15 PM

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

How do you create a map in Go?How do you create a map in Go?Apr 28, 2025 pm 05:14 PM

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

What is the difference between an array and a slice in Go?What is the difference between an array and a slice in Go?Apr 28, 2025 pm 05:13 PM

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.

How do you create a slice in Go?How do you create a slice in Go?Apr 28, 2025 pm 05:12 PM

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.

How do you create an array in Go?How do you create an array in Go?Apr 28, 2025 pm 05:11 PM

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.

What is the syntax for creating a struct in Go?What is the syntax for creating a struct in Go?Apr 28, 2025 pm 05:10 PM

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)

How do you create a pointer in Go?How do you create a pointer in Go?Apr 28, 2025 pm 05:09 PM

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

What are some benefits of using Go?What are some benefits of using Go?Apr 28, 2025 pm 05:08 PM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)