DEV Community

Masui Masanori
Masui Masanori

Posted on

[Windows] Try Golang 1

#go

Intro

I want to use Pion/WebRtc on Windows, but I've written a little Golang code.

So I try writing Golang in this time.

Environments

  • Windows 11 insider-preview-build-22598
  • Go ver.1.18.1 windows/amd64

GOPATH

Go installer sets GOPATH environment variable automatically.
But I can't use it like "$GOPATH" on PowerShell.

I have to write like "$ENV:GOPATH".

Modules

According to the README, I should execute "GO111MODULE=on go get ~" to run Pion/WebRtc examples.
But if I do that, I will get an error.

GO111MODULE=on : The term 'GO111MODULE=on' is not recognized as the name of a cmdlet, function, script file, or operabl
e program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
...
Enter fullscreen mode Exit fullscreen mode

Because from ver.1.13, GO111MODULE environment variable isn't set.
And I should "go mod init" instead it.

To run Pion/WebRtc examples, I just have to clone Pion/WebRtc and run them.

git clone https://github.com/pion/webrtc.git $ENV:GOPATH/src/github.com/pion/webrtc
cd $ENV:GOPATH/src/github.com/pion/webrtc/examples
go run examples.go --address localhost:8080
Enter fullscreen mode Exit fullscreen mode

Try creating a web application

Create a new project

According to the documents, I should create a new project in GOPATH.

mkdir $ENV:GOPATH/src/sample/webappsample
cd $ENV:GOPATH/src/sample/webappsample
go mod init sample/webappsample
Enter fullscreen mode Exit fullscreen mode

I create a .gitignore file by gitignore.io.

main.go

package main

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

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func main() {
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}
Enter fullscreen mode Exit fullscreen mode

Firewall

Now I have one problem.
When I execute "go run main.go", firewall security alerts shows every time.
Because the execution file path always be generated as new.

Image description

To avoid them, I can change the first "ListenAndServe" argument as "localhost:8080".

main.go

...
func main() {
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe("localhost:8080", nil))
}
Enter fullscreen mode Exit fullscreen mode

But if I do that, I can't access by IP address.
So if I change it to something like "0.0.0.0:8080", I get the first problem again :(

Perhaps, when I want to access from other devices, I should build first.

Show HTML files

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Go Sample</title>
        <meta charset="utf-8">
    </head>
    <body>
        <div>Hello world!</div>
    </body>
</html>
Enter fullscreen mode Exit fullscreen mode

main.go

package main

import (
    "html/template"
    "log"
    "net/http"
    "path/filepath"
    "sync"
)

type templateHandler struct {
    once     sync.Once
    filename string
    templ    *template.Template
}

func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // "sync.Once" executes only one time.
    t.once.Do(func() {
        // "Must()" wraps "ParseFiles()" results, so I can put it into "templateHandler.templ" directly
        t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename)))
    })
    t.templ.Execute(w, nil)
}

func main() {
    // In this sample, "ServeHTTP()" is called twice.
    // The second time is for loading "favicon.ico"
    http.Handle("/", &templateHandler{filename: "index.html"})
    log.Fatal(http.ListenAndServe("localhost:8080", nil))
}
Enter fullscreen mode Exit fullscreen mode

Load static files

How about loading static files like CSS, JavaScript, and so on?

I should add "http.Handle()" again.

main.go

...
func main() {
    // load templates/css/*
    http.Handle("/css/", http.FileServer(http.Dir("templates")))

    // In this sample, "ServeHTTP()" is called twice.
    // The second time is for loading "favicon.ico"
    http.Handle("/", &templateHandler{filename: "index.html"})
    log.Fatal(http.ListenAndServe("localhost:8080", nil))
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)