post image January 6, 2022 | 1 min Read

Go write to file

package main

import (
	"os"
	"io"
	"fmt"
	"log"
	"strings"
)

func main()  {
	// strongly typed channel
	name := "Jan"
	tpl := `
	<html>
		<body>
			<h1>Hi, this is:` + name + ` </h1> 
		</body>
	</html>
	`
	fmt.Printf("%v\n", tpl)

	newFile, err := os.Create("index.html")
	if err != nil {
		log.Fatal("error creating file")
	}
	defer newFile.Close()

	io.Copy(newFile, strings.NewReader(tpl))

}

author image

Jan Toth

I have been in DevOps related jobs for past 6 years dealing mainly with Kubernetes in AWS and on-premise as well. I spent quite a lot …

comments powered by Disqus