Skip to content

Latest commit

 

History

History
62 lines (45 loc) · 800 Bytes

03-files-go.md

File metadata and controls

62 lines (45 loc) · 800 Bytes

03-files-go

Write and read into a file

Create a new project with source code

mkdir 03-files-go
cd 03-files-go
go mod init files-go
touch main.go
package main

import (
	"fmt"
	"os"
)

func main() {

	file, err := os.Create("hello.txt")
	if err != nil {
		fmt.Println("😡", err)
		return
	}
	defer file.Close()

	_, err = file.WriteString("👋 Hello, World! 🌍")
	if err != nil {
		fmt.Println("😡", err)
		return
	}

	data, err := os.ReadFile("hello.txt")
	if err != nil {
		fmt.Println("😡", err)
	}

	fmt.Print(string(data))

}

Build

tinygo build -o main.wasm -target wasi ./main.go

ls -lh *.wasm

Run

wasmedge main.wasm

Yous should get an error

Read this to fix it: https://wasmedge.org/docs/start/build-and-run/cli/