Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Latest commit

 

History

History
75 lines (55 loc) · 1.48 KB

go-hello-world.md

File metadata and controls

75 lines (55 loc) · 1.48 KB

Go Hello World

Create a working web app with the code below!

package main

import (
    "fmt"
    "log"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World %s!", r.URL.Path[1:])
}

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

Steps

  • create a hello.go file using below
package main

import (
    "fmt"
    "log"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World %s!", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}
  • run the following command and leave console open
go run hello.go
  • in browser go to (replace YOUR_NAME with your name) http://localhost:8080/YOUR_NAME or in separate console
open http://localhost:8080/$USER

hello

Awesomeness!

So what did you do? When you ran go run hello.go, go compiled hello.go, created a binary in a temporary folder, and ran it.

Alternatively you could build and run it with

go build hello.go
./hello

In above, go compiled hello.go and created a binary in current path. You then can run it.

compiled server

In many other languages you need to deploy your app to a web app server or "bundle" a server with your app. With go the server is built and compiled into your app (binary).

gopher ninja