2019/NYC/golang

From IndieWeb

Using Go as a first language was a session at IndieWebCamp NYC 2019.

Video: ▢️38:30s

Notes archived from: https://etherpad.indieweb.org/golang


IndieWebCamp NYC 2019
Session: Using Go as a first language
When: 2019-10-05 15:15

Participants

Notes

  • fairly consistent experience
  • http server
  • golang.org to download and install
  • runs on linux,mac and windows (others as well)
  • after installing need to use two more tools - terminal and text/code editor
  • when in the terminal you can type in 'ls' to see the directories
  • when you type 'pwd' you can see what directory you are currently in
  • use mkdir 'some name' to create a new directory
  • type 'pwd' again to confirm that you are in your new directory
  • then type 'go version' to see if it is installed
  • go code will be done in text
  • create a new file in text editor and name it 'main.go'
  • go file will always have the following 3 parts - a package (and its name) , import statements , your code
  • need to write a function called main - this is where everything will start
  • to declare a function you need to write 'func' and the name ex : func main' ( ) { }
    • code will go in the { }
  • put spaces between the { }
  • go back to the terminal - need to come up with an import path
  • go supports custom import paths
  • in the terminal to create the import path you will need to type 'go mod init <website>/<project name>' e.g. go mod init first.last.com/mygoproject
  • it should say ' creating new go module'
  • command go run .( this will run the command in the current working directory)
  • go to 'godoc.org/net/http' we will scroll down to look at an example for a web server
  • put this into your code within the { } : { func main ( ) { http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) }) log.Fatal(http.ListenAndServe(":8080", nil)) }
  • now we need to import ---> under package main put 'import "net/http" ', 'import "log"', 'import "fmt" ', ' import "html"'

See Also