👊
Go tutorial for Pythoner
  • Introduction
  • Install and Run Go
  • The structure of Go
    • First time you meet him
    • Functions
    • Return multiple results
    • Logic Control
    • Break loop and enumerate
    • List (Array or Slice)
    • Dict (Map)
    • Struct
    • Class (Methods)
    • Packages
  • Features of Go
  • Go modules
  • Web Server
    • Serving static files
    • A simple webserver
  • Go for Android
  • Where to go next
Powered by GitBook
On this page
  • Make a folder
  • Initiate go mod
  • Add a package
  • Check your go.mod file
  • Add your code
  • Build your module
  • Use your program

Was this helpful?

Go modules

Make a folder

for example: godream

Initiate go mod

go mod init github.com/yingshaoxo/godream

Add a package

any package would be fine.

go get -u github.com/fogleman/gg

Check your go.mod file

you shall see something new inside of it.

Add your code

add a main.go file:

package main

import (
	"log"

	"github.com/fogleman/gg"
)

func main() {
	var W = 500
	var H = 500
	dc := gg.NewContext(W, H)
	if err := dc.LoadFontFace("font.ttf", 42); err != nil {
		panic(err)
	}

	const line_space = 1.5

	const text = "yingshaoxo is the best person in this world! \n no one can defeat him!"

	w, h := dc.MeasureMultilineString(text, line_space)
	log.Printf("w%f, h%f\n", w, h)

	W = int(w / 2)
	H = int(h)

	dc = gg.NewContext(W, H)
	dc.SetRGB(1, 1, 1)
	dc.Clear()
	dc.SetRGB(0, 0, 0)

	dc.DrawStringWrapped(text, float64(W)/2, float64(H)/2, 0.5, 0.5, float64(W), line_space, gg.AlignCenter)

	dc.SavePNG("out.png")
}

Build your module

go build

It will resolve all of your dependencies and build your module as a single binary for you.

Use your program

./godream
PreviousFeatures of GoNextWeb Server

Last updated 4 years ago

Was this helpful?