How to Draw a rectangle in Golang?
Below is a short program to create a PNG image of 200 X 200 size. In green variable RGBA value of green color assigned
Example
package main
import (
"image"
"image/color"
"image/draw"
"image/png"
"os"
"log"
)
func main() {
rectangle := "rectangle.png"
rectImage := image.NewRGBA(image.Rect(0, 0, 200, 200))
green := color.RGBA{0, 100, 0, 255}
draw.Draw(rectImage, rectImage.Bounds(), &image.Uniform{green}, image.ZP, draw.Src)
file, err := os.Create(rectangle)
if err != nil {
log.Fatalf("failed create file: %s", err)
}
png.Encode(file, rectImage)
}
Most Helpful This Week
Higher Order Functions in Golang
Golang Convert String into Snake Case
Dereferencing a pointer from another package
Example Function that takes an interface type as value and pointer?
What is Rune? How to get ASCII value of any character in Go?
How to iterate over a Map using for loop in Go?
Dynamic XML parser without Struct in Go
Simple example of Map initialization in Go
How to append text to a file in Golang?
Example to handle GET and POST request in Golang