In Go, you can use the net/http
package to create a basic web server and handle HTTP requests. Here's an example of creating hyperlinks to other web pages in HTML using Go:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
// Set the Content-Type header to HTML
w.Header().Set("Content-Type", "text/html")
// Write the HTML code with hyperlinks to other web pages
fmt.Fprintf(w, `<html>
<head>
<title>Hyperlinks Example</title>
</head>
<body>
<h1>Hyperlinks Example</h1>
<ul>
<li><a href="https://www.example.com">Example.com</a></li>
<li><a href="https://www.google.com">Google</a></li>
</ul>
</body>
</html>`)
}
This code sets up a basic HTTP server in Go, listening on port 8080. The handler
function is responsible for handling incoming requests. Inside the handler
function, the w
parameter is an interface that allows writing the response back to the client.
We first set the Content-Type
header to text/html
to indicate that we'll be sending an HTML response. Then, using the fmt.Fprintf
function, we write the HTML code with hyperlinks. In this example, we create an unordered list (<ul>
) with two list items (<li>
), each containing an anchor tag (<a>
) with an href
attribute linking to different web pages.
When you run the code and access http://localhost:8080
in your web browser, you'll see the HTML page with the hyperlinks to other web pages. Clicking on the links will take you to the respective web pages.