WebAssembly is a low-level, assembly-like language that brings near-native performance to browsers. WebAssembly allows developers to code in their familiar programming languages such as C/C++, Rust, Go, Python, and so on. The code is then compiled into the wasm binary format and can be executed in browsers .
Continue reading Basic example of running WebAssembly with Go
Tag: golang
mmap – an effective way of reading/writing large files
When operating with files in programming, we often read entire file content into physical memory. While writing the code doesn’t take much time, what happens behind the scenes is far more complex. During the process, your program has to invoke multiple system calls between buffers in kernel and user spaces back and forth which is considered inefficient. When reading a large file, mmap(memory-mapped file) is preferred in the terms of performance.
Continue reading mmap – an effective way of reading/writing large files
Deploy Grafana & Prometheus metrcs tracking system for your Go program
Deploy Golang function on Openfaas
this post will show you how to deploy a Sendtweet golang function in a OpenFaas Plaform.
Note of GoRoutines
Goroutines explain
in Go, each concurrently executing activity called a groutine
when a program starts, it has a groutine that calls the Main function
, we call it _Main groutine_
.
Goroutines have no notion of identity that is accessible to the programmer
Echo frameworks context bind twice without losing data
Retrieve data from echo middleware, may cause you get code=400, message=EOF
error.
Context.Bind
only can bind once, after the request passed the middleware,the request data reader is running out, Context.Bind()
function read request body data from the socket buffer, once you took it out, it just gone
that’s why it returns EOF error.
solution
func main() {
echoServer := echo.New()
echoServer.POST("/api", webfunc1, middleware1)
echoServer.Start(":8080")
}
func middleware1(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// read origin body bytes
var bodyBytes []byte
if c.Request().Body != nil {
bodyBytes, _ = ioutil.ReadAll(c.Request().Body)
// write back to request body
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
// parse json data
reqData := struct {
ID string `json:"id"`
}{}
err := json.Unmarshal(bodyBytes, &reqData)
if err != nil {
return c.JSON(400, "error json.")
}
fmt.Println(reqData.ID)
}
return next(c)
}
}
func webfunc1(c echo.Context) error {
reqData := struct {
Data string `json:"data"`
}{}
c.Bind(&reqData)
fmt.Println(reqData.Data)
return c.HTML(200, "200 OK.")
}
Golang: Read from an io.ReadWriter without losing its content