github.com/searKing/golang/go@v1.2.117/net/url/resolver.go (about) 1 // Copyright 2021 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package url 6 7 import ( 8 "context" 9 "net/url" 10 11 "github.com/searKing/golang/go/net/resolver" 12 ) 13 14 // ResolveWithTarget reset Host in url.Url by resolver.Target 15 // ResolveWithTarget always returns a new URL instance, 16 // even if the returned URL is identical to either the 17 // base or reference. 18 func ResolveWithTarget(ctx context.Context, u *url.URL, target string) (*url.URL, error) { 19 if u == nil { 20 return nil, nil 21 } 22 u2 := *u 23 if target == "" { 24 return &u2, nil 25 } 26 address, err := resolver.ResolveOneAddr(ctx, target) 27 if err != nil { 28 return nil, err 29 } 30 u2.Host = address.Addr 31 return &u2, nil 32 }