Home >Backend Development >Golang >How to Remove Diacritics from Strings in Go?

How to Remove Diacritics from Strings in Go?

Barbara Streisand
Barbara StreisandOriginal
2024-12-14 01:09:09193browse

How to Remove Diacritics from Strings in Go?

Remove Diacritics using Go

Transforming the string "žůžo" into "zuzo" involves removing all diacritics. This can be achieved using standard Go libraries described in Text Normalization in Go.

Code Implementation

The following code example demonstrates how to use these libraries:

package main

import (
    "fmt"
    "unicode"

    "golang.org/x/text/transform"
    "golang.org/x/text/unicode/norm"
)

func isMn(r rune) bool {
    return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}

func main() {
    t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
    result, _, _ := transform.String(t, "žůžo")
    fmt.Println(result) // prints: zuzo
}

By leveraging the capabilities of "transform" and "unicode/norm" libraries, you can effectively remove diacritics from UTF8 encoded strings in your Go programs.

The above is the detailed content of How to Remove Diacritics from Strings in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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