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
How to kill execution of goroutine?
How to reads and decodes JSON values from an input stream?
How to check if a map contains a key in Go?
Replace first occurrence of string using Regexp
How to import and alias package names?
Regular expression to extract numbers from a string in Golang
Different ways to validate JSON string
Example of Pointers with Struct
Golang import package inside package
Functions mess (recursive anonymous function) in Golang