Table of Contents
Question content
Solution
Home Backend Development Golang 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?

Feb 09, 2024 pm 11:20 PM
go language network programming overflow

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
Copy after login

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")
}
Copy after login

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
Copy after login

After inspection, it has the following disclaimer:

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

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The latest price of Bitcoin in 2018-2024 USD The latest price of Bitcoin in 2018-2024 USD Feb 15, 2025 pm 07:12 PM

Real-time Bitcoin USD Price Factors that affect Bitcoin price Indicators for predicting future Bitcoin prices Here are some key information about the price of Bitcoin in 2018-2024:

The most complete summary of historical price details since the birth of Bitcoin (the latest version in 2025) The most complete summary of historical price details since the birth of Bitcoin (the latest version in 2025) Feb 15, 2025 pm 06:45 PM

Important Node for Bitcoin Historical Price January 3, 2009: Genesis Block was generated, the first Bitcoin was generated, with a value of USD 0. October 5: The first Bitcoin transaction, a programmer bought two pizzas with 10,000 bitcoins, equivalent to $0.008. February 9, 2010: The Mt. Gox exchange went online and became the main platform for early Bitcoin trading. May 22: Bitcoin breaks through $1 for the first time. July 17: Bitcoin price plunged to $0.008, hitting an all-time low. February 9, 2011: Bitcoin price breaks through $10 for the first time. April 10: Mt. Go

What are the virtual currency pre-sale platforms? Top 10 virtual currency pre-sale exchanges What are the virtual currency pre-sale platforms? Top 10 virtual currency pre-sale exchanges Feb 15, 2025 pm 05:54 PM

The virtual currency pre-sale platform is an important channel for investors to invest in virtual currency projects. Through pre-sales, investors can have the opportunity to buy virtual coin tokens at prices below the market price, thereby achieving potential profit returns. This article will introduce the top ten virtual currency pre-sale exchanges, including Binance Launchpad, KuCoin Spotlight, Huobi Prime, Gate.io Startup, Bybit Launchpad, Polkastarter, TrustSwap, DuckDAO, BSCPad, and Seedify. These platforms provide a variety of virtual currency projects, covering DeFi, NFT, GameFi and other fields, for investment

In one article, learn about: What is the virtual currency fund rate and how to use the fund rate to trade In one article, learn about: What is the virtual currency fund rate and how to use the fund rate to trade Feb 15, 2025 pm 10:06 PM

Virtual currency funding rates are fees charged to traders holding positions in derivatives trading. It reflects a premium or discount between the spot market price and the futures contract price when the contract expires. When the spot price is higher than the futures price, the capital rate is negative, which means that traders who short positions pay fees to traders who long positions. On the contrary, when the spot price is lower than the futures price, the capital rate is positive, which means that traders who do long positions pay fees to traders who do short positions.

Analysis of the most promising virtual currency in 2025 Top 10 potential virtual currency inventory in 2025 Analysis of the most promising virtual currency in 2025 Top 10 potential virtual currency inventory in 2025 Feb 15, 2025 pm 05:51 PM

The virtual currency market is constantly evolving and exciting growth is expected in the coming years. In 2025, some cryptocurrencies are expected to stand out and become the most promising investments in the space. This article analyzes some of the most promising virtual currencies in 2025, covering their unique capabilities, growth potential and possibilities that impact the future. These currencies include Ethereum, Bitcoin, Cardano, Polkadot and Binance Coin, which play a key role in the development of decentralized finance, smart contracts and blockchain technologies. Understanding the potential of these virtual currencies, investors can be prepared to seize the opportunities brought by the virtual currency market in 2025.

Virtual currency trading platform entrance 2025 Global Regular Virtual Currency Trading Platform Official Website Portal Virtual currency trading platform entrance 2025 Global Regular Virtual Currency Trading Platform Official Website Portal Feb 15, 2025 pm 04:12 PM

In 2025, the pattern of virtual currency trading platforms underwent a major change, and formal platforms dominated. These officially recognized exchanges provide users with a safe and reliable trading environment and meet strict standards set by regulators. As cryptocurrencies play an increasingly important role in the global financial system, formal virtual currency trading platforms will become a key factor in promoting their widespread adoption and popularity.

How much is Bitcoin worth now 2025 How much is Bitcoin worth in January How much is Bitcoin worth now 2025 How much is Bitcoin worth in January Feb 15, 2025 pm 06:18 PM

At this stage, the value of Bitcoin is affected by multiple factors, including supply and demand relationships, technological development, regulatory policies and macroeconomic situation. As of January 2025, Bitcoin’s valuation may fluctuate due to changes in these factors.

Where to buy melania coins? Melania Coin Trading Exchange 2025 Ranking Where to buy melania coins? Melania Coin Trading Exchange 2025 Ranking Feb 15, 2025 pm 05:03 PM

The article focuses on buying and selling information about Melania coins and focuses on the ranking of potential exchanges in 2025. The article explores different channels for obtaining Melania coins and provides investors with insights on the best place to buy.

See all articles