2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Golangの個人的チートシート

Last updated at Posted at 2021-01-10

#この記事について
完全に自分用に書いているので説明は少なめで簡単な処理のものが多いですが、備忘録的に使うので随時更新します。

##stringからのキャスト
###stringint

func atoi(a string) (b int) {
    b, err := strconv.Atoi(a)
    if err != nil {
        panic(err)
    }
    return
}

###半角スペース区切りの string[]string

func aToSlice(a string) (b []string) {
    b = strings.Fields(strings.TrimSpace(a))
    return
}

###半角スペース区切りの string[]int

func aToIntSlice(a string) (b []int) {
    c := strings.Fields(strings.TrimSpace(a))
    for _, v := range c {
        d := atoi(v)
        b = append(b, d)
    }
    return
}

###半角スペース区切りの string[]float64

func aToFloat64Slice(a string) (b []float64) {
    c := strings.Fields(strings.TrimSpace(a))
    for _, v := range c {
        d, _ := strconv.ParseFloat(v, 64)
        b = append(b, d)
    }
    return
}

##スライスから別の型のスライスへのキャスト

###[]string[]int

func toIntSlice(a []string) (b []int) {
    for _, s := range a {
        i := atoi(s)
        b = append(b, i)
    }
    return
}

###[]int[]string

func toStrSlice(a []int) (b []string) {
    for _, i := range a {
        s := strconv.Itoa(i)
        b = append(b, s)
    }
    return
}

##その他
###絶対値を int で返す

func abs(a int) (b int) {
    b = int(math.Abs(float64(a)))
    return
}

###map[int]int からvalueの最大値を返す

func findMaxValue(m map[int]int) (maxValue int) {
    var max int
    var maxIndex int
    for i, v := range m {
        if max <= v {
            max = v
            maxIndex = i
        }
    }
    maxValue = m[maxIndex]
    return
}

###map[int]int から最大値を持つkeyのみのスライスを返す
Goの連想配列(map)は、範囲ループを使うと順番が実行ごとに異なるので昇順でソート返す。

func findMaxValueKeys(m map[int]int) (maxKeys []int) {
    maxValue := findMaxValue(m)
    
    for i, v := range m {
        if v == maxValue {
            maxKeys = append(maxKeys, i)
        }
    }
    sort.Slice(maxKeys, func (i, j int) bool { return maxKeys[i] < maxKeys[j] })
    return
}

アルファベットを順番に出力

for i := 0; i < 26; i++ {
    fmt.Print(string('a'+i))
}

文字列を逆順にする

func reverseString(s string) (rs string) {
	for i := len(s); i > 0; i-- {
		rs += s[i-1 : i]
	}
	return
}

数字を3桁ずつカンマ区切りにする (intstring

ループ1回でできそうだけど

func decorateNum(num int) (decoratedNum string) {
	strNum := strconv.Itoa(num)

	revStrNum = reverseString(strNum)
	var revDecoratedNum string
	for i := 0; i < len(strNum); i++ {
		revDecoratedNum += revStrNum[i : i+1]
		if (i+1)%3 == 0 && i+1 < len(strNum) {
			revDecoratedNum += ","
		}
	}

	for i := len(revDecoratedNum); i > 0; i-- {
		decoratedNum += revDecoratedNum[i-1 : i]
	}

	return
}

ひらがなを半角カタカナに変換

いい感じのパッケージを見つけられなかった

func hiraganaToSingle(hiraganaInput string) (singleOutput string) {
	hiragana := "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをんぁぃぅぇぉっゃゅょゔがぎぐげござじずぜぞだぢづでどばびぶべぼぱぴぷぺぽ"
	single := "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンァィゥェォッャュョウカキクケコサシスセソタチツテトハヒフヘホハヒフヘホ"

	for i := 0; i < len([]rune(hiraganaInput)); i++ {
		for j := 0; j < len([]rune(hiragana)); j++ {
			if string([]rune(hiraganaInput)[i:i+1]) == string([]rune(hiragana)[j:j+1]) {
				singleOutput += string([]rune(single)[j : j+1])
				if j > 54 && j < 76 {
					singleOutput += "゙"
				} else if j >= 76 {
					singleOutput += "゚"
				}
			}
		}
	}

	return
}
2
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?