Encode / decode URLs

后端 未结 8 1485
攒了一身酷
攒了一身酷 2020-11-28 03:46

What\'s the recommended way of encoding and decoding entire URLs in Go? I am aware of the methods url.QueryEscape and url.QueryUnescape, but they d

相关标签:
8条回答
  • 2020-11-28 04:41

    How about this:

    template.URLQueryEscaper(path)
    
    0 讨论(0)
  • 2020-11-28 04:43

    If someone wants to get the exact result compare to the JS encodeURIComponent Try my function it's dirty but works well.

    https://gist.github.com/czyang/7ae30f4f625fee14cfc40c143e1b78bf

    // #Warning! You Should Use this Code Carefully, and As Your Own Risk.
        package main
    
        import (
        "fmt"
        "net/url"
        "strings"
    )
    /*
    After hours searching, I can't find any method can get the result exact as the JS encodeURIComponent function.
    In my situation I need to write a sign method which need encode the user input exact same as the JS encodeURIComponent.
    This function does solved my problem.
    */
    func main() {
        params := url.Values{
            "test_string": {"+!+'( )*-._~0-                                                                    
    0 讨论(0)
提交回复
热议问题