Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Web Development Basics

23. Web Development Basics

Let's dive into the basics of web development using the Go programming language. We'll cover setting up a basic web server, handling routes, serving static files, and creating dynamic templates.

Setting Up a Basic Web Server:

To start, you need to import the necessary packages ('net/http') and define route handlers using the'http.HandleFunc'function.

package main

import (
"fmt"
"net/http"
)

func main() {
http.HandleFunc("/", helloHandler)
http.ListenAndServe(":8080", nil)
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, Go Web!")
}

 

In this example, when you visit  'http://localhost:8080/', you'll see the message "Hello, Go Web!" displayed in your browser.

Handling Routes:

You can define different route handlers for various URLs using the 'http.HandleFunc'function.

func main() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/about", aboutHandler)
http.ListenAndServe(":8080", nil)
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Welcome to the Home Page!")
}

func aboutHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "This is the About Page.")
}

 

Now, visiting 'http://localhost:8080/' displays "Welcome to the Home Page!", and visiting 'http://localhost:8080/about' displays "This is the About Page."

Serving Static Files:

You can serve static files like HTML, CSS, and JavaScript by using the 'http:FileServer'function.

func main() {
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	http.ListenAndServe(":8080", nil)
}

 

Create a directory named "static" in the same location as your Go code. Place your static files there, such as'static/index.html', 'static/style.css', etc.

Creating Dynamic Templates:

To generate dynamic HTML content, you can use Go's built-in template package.

package main

import (
   "html/template"
   "net/http"
)

type PageData struct {
   Title string
   Content string
}

func main() {
   http.HandleFunc("/", dynamicHandler)
   http.ListenAndServe(":8080", nil)
}

func dynamicHandler(w http.ResponseWriter, r *http.Request) {
   data := PageData{
       Title: "Dynamic Page",
       Content: "This is dynamically generated content using Go templates.",
   }

   tmpl, err := template.New("index").Parse(`
   <!DOCTYPE html>
   <html>
   <head>
       <title></title>
   </head>
   <body>
       <h1></h1>
       <p></p>
   </body>
   </html>
   `
)

   if err != nil {
       http.Error(w, "Internal Server Error", http.StatusInternalServerError)
       return
   }

   err = tmpl.Execute(w, data)
   if err != nil {
       http.Error(w, "Internal Server Error", http.StatusInternalServerError)
   }
}        

 

Visiting 'http://localhost:8080/' will show the dynamically generated page with the title "Dynamic Page" and the provided content.

Summary:

  • Setting up a basic web server involves using the  'net/http'package.
  • Handling routes is done using the 'http.HandleFunc' function.
  • Serving static files can be accomplished with the  'http.FileServer' function.
  • Creating dynamic templates uses the built-in 'html/template'package.
  • Go provides a simple and effective way to build web applications with the standard library.

Remember, this is just the beginning of web development with Go. As you explore further, you can integrate databases, handle forms, implement user authentication, and much more to build fully-featured web applications.