I am a golang beginner and I have a package level variable:
var yellow color.RGBA
I want to initialize it in a function so I do this (no compiler warnings):
func setColors() {
yellow = color.RGBA{R: 0xff, G: 0xff, B: 0x00, A: 0xff}
}
If I do this in my function, I get a "Unnamed field initialization" compiler warning:
yellow = color.RGBA{0xff, 0xff, 0x00, 0xff}
But my project-level variable allows me to do both of these:
var yellow = color.RGBA{0xff, 0xff, 0x00, 0xff}
var yellow = color.RGBA{R: 0xff, G: 0xff, B: 0x00, A: 0xff}
Why can I omit the field names (R, G, B, A) in the project-level initialization but not in the function?
24-Jan Update -- Complete Code Example
This is where my variable initialization problem is happening.
This application displays a dynamic lissajous figure in a web browser. The lissajous line color is yellow.
You need to start the app with a command line parameter web After starting the app, open a browser and point it to http://localhost:8000/
Line reference #3 (func setColors(), end of code sample) is supposed to set the yellow variable (line reference #2). Line reference #3 is where I get the "Unnamed field initialization" compiler warning.
When I run the app as is from within Intellij, var yellow does not get set and the lissajous figure does not get drawn correctly in the browser.
However if I use line reference #4 instead of #3, the application works fine.
Line reference #1 works fine, but I need to know why reference #3 is not working.
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
// Run with "web" command-line argument for web server.
// See page 13.
// Lissajous generates GIF animations of random Lissajous figures.
package main
import (
"image"
"image/color"
"image/gif"
"io"
"math"
"math/rand"
"os"
"time"
"net/http"
"log"
"fmt"
"reflect"
"strconv"
)
var palette = []color.Color{color.Black, yellow }
var red color.RGBA= color.RGBA{0xff, 0x00, 0x00, 0xff}
var green color.RGBA = color.RGBA{0x00, 0xff, 0x00, 0xff}
// Line reference #1
//var yellow = color.RGBA{R: 0xff, G: 0xff, B: 0x00, A: 0xff}
// Line reference #2
var yellow color.RGBA
var blue color.RGBA = color.RGBA{0x00, 0x00, 0xff, 0xff}
const (
whiteIndex = 0 // first color in palette
yellowIndex = 1 // next color in palette
)
func main() {
// The sequence of images is deterministic unless we seed
// the pseudo-random number generator using the current time.
// Thanks to Randall McPherson for pointing out the omission.
rand.Seed(time.Now().UTC().UnixNano())
setColors()
if len(os.Args) > 1 && os.Args[1] == "web" {
handler := func(w http.ResponseWriter, r *http.Request) {
lissajous(w, r)
}
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
return
}
lissajous(os.Stdout, nil)
}
func lissajous(out io.Writer, r *http.Request) {
const (
//cycles = 5 // number of complete x oscillator revolutions
res = 0.001 // angular resolution
//size = 100 // image canvas covers [-size..+size]
//nframes = 64 // number of animation frames
//delay = 8 // delay between frames in 10ms units
)
// Get query parameters
cycles := getQueryParameterFloat("cycles", r, 5)
size := getQueryParameterInteger("size", r, 100)
sizeFloat := float64(size)
nframes := getQueryParameterInteger("nframes", r, 64)
delay := getQueryParameterInteger("delay", r, 8)
fmt.Println("cycles type:", reflect.TypeOf(cycles))
freq := rand.Float64() * 3.0 // relative frequency of y oscillator
anim := gif.GIF{LoopCount: nframes}
phase := 0.0 // phase difference
for i := 0; i < nframes; i++ {
rect := image.Rect(0, 0, 2*size+1, 2*size+1)
img := image.NewPaletted(rect, palette)
for t := 0.0; t < cycles*2*math.Pi; t += res {
x := math.Sin(t)
y := math.Sin(t*freq + phase)
img.SetColorIndex(size+int(x*sizeFloat+0.5), size+int(y*sizeFloat+0.5),
yellowIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
}
// getQueryParameter returns the value of a parameter in the query string.
// If an error occurs, the function returns the default value passed in.
func getQueryParameterFloat(name string, r *http.Request, defaultVal float64) float64 {
v := r.URL.Query().Get(name)
v2, err := strconv.ParseFloat(v, 64)
if err != nil {
return defaultVal
} else {
return v2
}
}
func getQueryParameterInteger(name string, r *http.Request, defaultVal int) int {
v := r.URL.Query().Get(name)
v2, err := strconv.Atoi(v)
if err != nil {
return defaultVal
} else {
return v2
}
}
func setColors() {
// Line reference #3 When I use this, the application does not function correctly
yellow = color.RGBA{0xff, 0xff, 0x00, 0xff}
// Line refernce #4 When I use this, the application **does** function correctly.
//yellow = color.RGBA{R: 0xff, G: 0xff, B: 0x00, A: 0xff}
}
Where are you getting this error? If this is in Intellij with go-idea plugin. Then this has been recently fixed as you can see here.
Just tried your question out with the following code:
package main
import (
"fmt"
"image/color"
)
var yellow color.RGBA
func main() {
fmt.Println(yellow)
setColors()
fmt.Println(yellow)
}
func setColors() {
yellow = color.RGBA{R: 0xff, G: 0xff, B: 0x00, A: 0xff}
}
With the output:
{0 0 0 0}
{255 255 0 255}
It seems to be working for me, I'm not sure if I wrote my code differently to yours.