Creating, copying, and running a Go web application on your first EC2 instance
Once we have an EC2 instance ready with the required libraries installed, we can simply copy the application using the secure copy protocol and then run it using the go run
command, which we will be covering in this recipe.
How to do it…
- Create
http-server.go
, where we will create a simple HTTP server that will renderHello World!
browsinghttp://ec2-instance-public-dns:80
or executingcurl -X GET http://ec2-instance-public-dns:80
from the command line, as follows:
package main import ( "fmt" "log" "net/http" ) const ( CONN_PORT = "80" ) func helloWorld(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello World!") } func main() { http.HandleFunc("/", helloWorld) err := http.ListenAndServe(":"+CONN_PORT, nil) if err != nil { log.Fatal("error starting http server : ", err) return } }
With everything in place, the directory structure should look like the following:

- Copy
http...