3
3

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 5 years have passed since last update.

Go言語でURLエンコード

Posted at

こちらを参考に作成しました。JavaScriptにおけるURLエンコードの処理

urlencode.go
package main

import (
  "fmt"
)

func urlencode(s string) (result string){
  for _, c := range(s) {
    if c <= 0x7f { // single byte 
      result += fmt.Sprintf("%%%X", c)
    } else if c > 0x1fffff {// quaternary byte
      result += fmt.Sprintf("%%%X%%%X%%%X%%%X",
        0xf0 + ((c & 0x1c0000) >> 18),
        0x80 + ((c & 0x3f000) >> 12),
        0x80 + ((c & 0xfc0) >> 6),
        0x80 + (c & 0x3f),
      )
    } else if c > 0x7ff { // triple byte
      result += fmt.Sprintf("%%%X%%%X%%%X",
        0xe0 + ((c & 0xf000) >> 12),
        0x80 + ((c & 0xfc0) >> 6),
        0x80 + (c & 0x3f),
      )
    } else { // double byte
      result += fmt.Sprintf("%%%X%%%X",
        0xc0 + ((c & 0x7c0) >> 6),
        0x80 + (c & 0x3f),
      )
    }
  }

  return result
}

func main() {
  fmt.Println(urlencode("新宿しんじゅくシンジュクsinjuku"))
}
3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?