1. Sections of a URL

URLs have quite a few sections. Some are required, some are not.

Assignment

Let's use the url.URL structarrow-up-right again. This time, we'll parse a URL string and add all the different parts to a custom ParsedURL struct. We'll learn more about each part later.

Complete the newParsedURL function. It should return a ParsedURL with all the parts of a URL. For example, given this URL:

http://testuser:testpass@testdomain.com:8080/testpath?testsearch=testvalue#testhash

newParsedURL should return a struct with these fields:

return ParsedURL{
	protocol: "http",
	username: "testuser",
	password: "testpass",
	hostname: "testdomain.com",
	port:     "8080",
	pathname: "/testpath",
	search:   "testsearch=testvalue",
	hash:     "testhash",
}

Use these fields and methods of the URL struct:

  • Scheme

  • User.Username()

  • User.Password()arrow-up-right - this method returns two values: the password if set and whether it was set

  • Hostname()

  • Port()

  • Path

  • RawQuery

  • Fragment

Here's what each field maps to:

Protocol (Scheme)

Username

Password (Two Return Values!)

Hostname

Port

Pathname (Path)

Search (RawQuery)

Hash (Fragment)

Visual Breakdown of URL

Why Password Returns Two Values

The boolean tells you if a password was actually set:

Since you're just getting the password, you ignore the boolean with _:

Completed implementation

Key Points to Remember

  • Password returns two values β€” use pass, _ := parsedUrl.User.Password()

  • Path vs RawQuery β€” Path is pathname, RawQuery is search

  • Fragment β€” this is the hash (part after #)

  • Scheme β€” this is the protocol