search
HomeBackend DevelopmentGolangWill 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?

Feb 09, 2024 pm 11:20 PM
go languagenetwork programmingoverflow

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. If there is any infringement, please contact admin@php.cn delete
Choosing Between Golang and Python: The Right Fit for Your ProjectChoosing Between Golang and Python: The Right Fit for Your ProjectApr 19, 2025 am 12:21 AM

Golangisidealforperformance-criticalapplicationsandconcurrentprogramming,whilePythonexcelsindatascience,rapidprototyping,andversatility.1)Forhigh-performanceneeds,chooseGolangduetoitsefficiencyandconcurrencyfeatures.2)Fordata-drivenprojects,Pythonisp

Golang: Concurrency and Performance in ActionGolang: Concurrency and Performance in ActionApr 19, 2025 am 12:20 AM

Golang achieves efficient concurrency through goroutine and channel: 1.goroutine is a lightweight thread, started with the go keyword; 2.channel is used for secure communication between goroutines to avoid race conditions; 3. The usage example shows basic and advanced usage; 4. Common errors include deadlocks and data competition, which can be detected by gorun-race; 5. Performance optimization suggests reducing the use of channel, reasonably setting the number of goroutines, and using sync.Pool to manage memory.

Golang vs. Python: Which Language Should You Learn?Golang vs. Python: Which Language Should You Learn?Apr 19, 2025 am 12:20 AM

Golang is more suitable for system programming and high concurrency applications, while Python is more suitable for data science and rapid development. 1) Golang is developed by Google, statically typing, emphasizing simplicity and efficiency, and is suitable for high concurrency scenarios. 2) Python is created by Guidovan Rossum, dynamically typed, concise syntax, wide application, suitable for beginners and data processing.

Golang vs. Python: Performance and ScalabilityGolang vs. Python: Performance and ScalabilityApr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Golang vs. Other Languages: A ComparisonGolang vs. Other Languages: A ComparisonApr 19, 2025 am 12:11 AM

Go language has unique advantages in concurrent programming, performance, learning curve, etc.: 1. Concurrent programming is realized through goroutine and channel, which is lightweight and efficient. 2. The compilation speed is fast and the operation performance is close to that of C language. 3. The grammar is concise, the learning curve is smooth, and the ecosystem is rich.

Golang and Python: Understanding the DifferencesGolang and Python: Understanding the DifferencesApr 18, 2025 am 12:21 AM

The main differences between Golang and Python are concurrency models, type systems, performance and execution speed. 1. Golang uses the CSP model, which is suitable for high concurrent tasks; Python relies on multi-threading and GIL, which is suitable for I/O-intensive tasks. 2. Golang is a static type, and Python is a dynamic type. 3. Golang compiled language execution speed is fast, and Python interpreted language development is fast.

Golang vs. C  : Assessing the Speed DifferenceGolang vs. C : Assessing the Speed DifferenceApr 18, 2025 am 12:20 AM

Golang is usually slower than C, but Golang has more advantages in concurrent programming and development efficiency: 1) Golang's garbage collection and concurrency model makes it perform well in high concurrency scenarios; 2) C obtains higher performance through manual memory management and hardware optimization, but has higher development complexity.

Golang: A Key Language for Cloud Computing and DevOpsGolang: A Key Language for Cloud Computing and DevOpsApr 18, 2025 am 12:18 AM

Golang is widely used in cloud computing and DevOps, and its advantages lie in simplicity, efficiency and concurrent programming capabilities. 1) In cloud computing, Golang efficiently handles concurrent requests through goroutine and channel mechanisms. 2) In DevOps, Golang's fast compilation and cross-platform features make it the first choice for automation tools.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function