Example of converting hiragana to katakana in Go

This Go example program demonstrates converting hiragana into katakana.

package main

import (
        "fmt"
        "strings"
)

func hira2kata (hira rune) rune {
        if (hira >= 'ぁ' && hira <= 'ゖ') || (hira >= 'ゝ' && hira <= 'ゞ') {
                return hira + 0x60
        }
        return hira
}

func Hira2kata(hira string) string {
        return strings.Map(hira2kata, hira)
}

func kata2hira (kata rune) rune {
        if (kata >= 'ァ' && kata <= 'ヶ') || (kata >= 'ヽ' && kata <= 'ヾ') {
                return kata - 0x60
        }
        return kata
}

func Kata2hira(kata string) string {
        return strings.Map(kata2hira, kata)
}

func main() {
        hiragana := "あいうえおかきくけこ"
        katakana := Hira2kata(hiragana)
        hiragana2 := Kata2hira(katakana)
        fmt.Printf ("%s\n%s\n", katakana, hiragana2)
}

(download)

The output looks like this:

アイウエオカキクケコ
あいうえおかきくけこ


Copyright © Ben Bullock 2009-2017. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com). / Privacy / Disclaimer