Home > Backend Development > Golang > How to improve segmentation logic in golang

How to improve segmentation logic in golang

PHPz
Release: 2024-02-09 10:00:41
forward
935 people have browsed it

How to improve segmentation logic in golang

In golang, splitting a string is a common operation. However, sometimes we may encounter some splitting logic problems. So, how to improve the segmentation logic in golang? In this article, PHP editor Yuzai will introduce you to some practical tips and methods to help you optimize and improve the segmentation logic in golang, making your code more efficient and reliable. Let’s explore together!

Question content

Consider the following examples as possible values ​​in the index:

values := [5]string{"32.5ms", "32.5 ms", "32.5%", "32.5 %", "none"}
Copy after login

Note that the original value may or may not have spaces for the unit of measurement (32.5%, 32.5 %, 32.5 %, etc.)

I need to separate out the floating point value and unit of measurement (%, ms, etc.) from the original value, the following code, gets the result I want, but I want to know Is there a cleaner way to perform the same logic, perhaps without the need for regular expressions.

package main

import (
    "fmt"
    "regexp"
    "strings"
)

func main() {

    regexFloatNumbers := regexp.MustCompile(`[-]?\d[\d,]*[\.]?[\d{2}]*`)

    values := [5]string{"32.5ms", "32.5 ms", "32.5%", "32.5 %", "none"}

    for _, value := range values {

        if regexFloatNumbers.MatchString(value) {
            floatValue := regexFloatNumbers.FindString(value)
            fmt.Printf("ORIGINAL VALUE: %q\n", value)
            fmt.Printf("UNIT: %q\n", strings.TrimSpace(regexFloatNumbers.Split(value, -1)[1]))
            fmt.Printf("FLOAT VALUE: %v\n\n", floatValue)
        } else {
            fmt.Printf("float value for %v has not being found!", value)
        }

    }

}
Copy after login

Solution

Regular expressions seem to fit well here. Personally I would use subgroups for this, like this (I also cleaned up your regex, it had some unnecessary syntax and it looked like [\d{2} ]* misspelling):

regexFloatNumbers := regexp.MustCompile(`(-?\d[\d,]*\.?\d*) *(.*)`)

// ...

floatValue := regexFloatNumbers.FindStringSubmatch(value)
fmt.Printf("ORIGINAL VALUE: %q\n", value)
fmt.Printf("UNIT: %q\n", floatValue[1])
fmt.Printf("FLOAT VALUE: %v\n\n", floatValue[2])
Copy after login

https://www.php.cn/link/8af0c75579a632c9207e51657de43c7c

The above is the detailed content of How to improve segmentation logic in golang. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template