👊
Go tutorial for Pythoner
  • Introduction
  • Install and Run Go
  • The structure of Go
    • First time you meet him
    • Functions
    • Return multiple results
    • Logic Control
    • Break loop and enumerate
    • List (Array or Slice)
    • Dict (Map)
    • Struct
    • Class (Methods)
    • Packages
  • Features of Go
  • Go modules
  • Web Server
    • Serving static files
    • A simple webserver
  • Go for Android
  • Where to go next
Powered by GitBook
On this page

Was this helpful?

  1. Web Server

A simple webserver

package main

import ("fmt"
        "net/http")

func index_handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, yingshaoxo!")
}

func about_handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "This is based on Golang.")
}

func main() {
    http.HandleFunc("/", index_handler)
    http.HandleFunc("/about/", about_handler)

    fmt.Println("http://127.0.0.1:5000")
    http.ListenAndServe(":5000", nil)
}

It's much like python's flask package:

/ for index of website.

/about/ for about page.

There just one thing you should care about: *.

It reads value from a pointer address (http.Request).

PreviousServing static filesNextGo for Android

Last updated 6 years ago

Was this helpful?