Home  >  Article  >  Backend Development  >  Will Go 1.21 include the ability to host http via WebAssembly? how?

Will Go 1.21 include the ability to host http via WebAssembly? how?

王林
王林forward
2024-02-09 23:20:251116browse

Go 1.21 会包含通过 WebAssembly 托管 http 的功能吗?如何?

php editor Xiaoxin is happy to answer your question about whether Go 1.21 will include the function of hosting HTTP through WebAssembly. Currently, Go 1.21 version has not been officially released, so we cannot determine its specific features. However, based on what we know about the Go language, Go is very focused on the development of WebAssembly and network programming, so it is very likely that the function of hosting HTTP through WebAssembly will be added in a future version. This will provide developers with more flexible and efficient web development tools. We recommend that you continue to pay attention to the official release information of the Go language to get the latest updates and features.

Question content

I want to try http server through webassembly on go. I think go 1.20 does not support compiling go for web assembly outside of the browser, and the net/http library is not included in tinygo.

After reading https://stackoverflow.com/a/76091829 (thanks to @tachyonicbytes) I tried using gotip to accomplish this, but whenever I try to start the server (or whatever blocks/ Wait function), I get the error: Fatal error: All goroutines are sleeping - deadlock! . I tried moving things into a goroutine with an await function, but that either simply ends the function or gives the same error. This is how I run it:

go install golang.org/dl/gotip@latest
gotip download
goos=wasip1 goarch=wasm gotip build -o server.wasm server.go && wasm3 server.wasm

This is an example server.go:

package main

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

func main() {
    s := http.Server{
        Addr: ":8080",
        Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            w.Write([]byte("Hello, World!"))
        }),
    }

    fmt.Println("about to serve")
    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        err := s.ListenAndServe()
        if err != nil {
            fmt.Printf("Unable to serve: %v\n", err)
        }
        wg.Done()
        fmt.Println("serving stopped")
    }()
    wg.Wait()
    fmt.Println("started up server")
}

So, is this just because go 1.21 is a wip, because I can't understand the correct way to launch a blocking function, or because go 1.21 doesn't support such a thing?

I try to start the go server in the server-side webassembly runner wasm3 on an intel mac. I expected it to serve http but found that it either threw an error or exited immediately.

Solution

I’m glad to help you!

Unfortunately, it doesn't look like wasm networking will be part of go 1.21. Implementing networking in wasm is a bit complicated. Running your code I got this line:

sdk/gotip/src/net/net_fake.go:229

After inspection, it has the following disclaimer:

// Fake networking for js/wasm and wasip1/wasm.
// This file only exists to make the compiler happy.

The difficulty with this is that wasi only provides partial support for sockets, so wasi does not yet have a complete Berkeley socket.

The good news is that you can actually do http, but in tinygo. tinygo provides partial support for the go net/http package and its driver.

If you want to see some practical uses of it, I'm currently trying to port this using tinygo project to wasm. If I remember correctly I've got it working, but it's been a while and I'm sure I haven't finished the conversion yet. Maybe it's not possible for the time being.

Another thing is wasm3 Although there is a partial wasi implementation, the socket part may not be implemented. I recommend using some other runtimes as well, such as wasmtime, wasmer, wasmedge or wazero as @gedw99 suggested. wasmedge There is good support for sockets, but in your case the compiler is actually the problem.

The above is the detailed content of Will Go 1.21 include the ability to host http via WebAssembly? how?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete