Skip to content

proposal: net/url: url manipulation after creation/parsing #40239

@CommoDor64

Description

@CommoDor64

What version of Go are you using (go version)?

go1.14.2 darwin/amd64

Does this issue reproduce with the latest release?

It is reproducible also on Playground, go1.14.4

What operating system and processor architecture are you using (go env)?

GO111MODULE="on"
GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"

What did you do?

net/url presents weird behaviour in some situations

Now, the issue I spot, is that there is no way to re-parse the same struct on url structure change.
All changes to the url.URL struct are direct property manipulation and not with proper struct fucntions

package main

import (
	"fmt"
	"net/url"
)

func parseURLWithoutScheme() *url.URL {
	u, err := url.Parse("somedomain.com/firstpath")
	if err != nil {
		return nil
	}
	return u
}

func parseURLWithScheme() *url.URL {
	u, err := url.Parse("https://somedomain.com/firstpath")
	if err != nil {
		return nil
	}
	return u
}

func main() {

	fmt.Println("1 path WITH scheme intialized:", parseURLWithScheme().Path) 
        // output: /firstpath

	u := parseURLWithoutScheme()
	fmt.Println("2 path WITH NO scheme intialized:", u.Path) 
        // output: somedomain.com/firstpath
	
	u.Scheme = "https"
	fmt.Println("3 path WITH NO scheme intialized, scheme was added later:", parseURLWithoutScheme().Path)
        // output: somedomain.com/firstpath

	u, _ = url.Parse(u.String())
	fmt.Println("4 same path of url struct in example 3, but reparsed into url.URL:", u.Path)
        // output: /firstpath

}

Available here
https://play.golang.org/p/3SkxRVIgmYS

The inability to change the url structure is such a way after creation is quirky.

Proposal

Add manipulation methods such as

func (*URL) SetPath(path string) { // re-parse and set properties respectively}

or allowing deep copy

func (*URL) DeepCopy() *URL { // re-parse and create a new URL struct}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions