0
0

More than 1 year has passed since last update.

GoでURLの全角・半角空白を処理する

Last updated at Posted at 2023-08-28

TL;DR

検索機能などを実装するとき、クエリパラメータで送られてきた検索ワードを一旦配列に落とし込みたい。
けど、ユーザーによって使われるスペースの全角・半角は人それぞれ。

方法

1.クエリをデコードする

parsedKeyword, err := url.QueryUnescape(keyword)
if err != nil {
    return nil, err
}

2.全角をすべて半角に置き換え

singleByteKeywords := strings.Replace(parsedKeyword, " ", " ", -1)

3.Splitする

keywords := strings.Split(singleByteKeywords, " ")
0
0
0

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
0
0