Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant struct in Go

Tags:

go

Why can't I create constant struct?

const FEED_TO_INSERT = quzx.RssFeed{ 0,                     "",                     "desc",                     "www.some-site.com",                     "upd_url",                     "img_title",                     "img_url",                     0,                     0,                     0,                     0,                     0,                     100,                     "alt_name",                     1,                     1,                     1,                     "test",                     100,                     100,                     0 } 

.\rss_test.go:32: const initializer quzx.RssFeed literal is not a constant

like image 438
ceth Avatar asked Apr 12 '17 11:04

ceth


People also ask

Can struct to be constant golang?

Go does not support constants in structs. which is an invalid syntax and also has no semantic meaning. If you want to assign each game variable a game ID, you can add an unexported field to the struct, as well as a method that returns the ID value.

How do you declare a constant in Golang?

To declare a constant and give it a name, the const keyword is used. Constants cannot be declared using the := syntax.


1 Answers

Because Go does not support struct constants (emphasis mine)

There are boolean constants, rune constants, integer constants, floating-point constants, complex constants, and string constants. Rune, integer, floating-point, and complex constants are collectively called numeric constants.

Read more here: https://golang.org/ref/spec#Constants

like image 171
mkopriva Avatar answered Oct 01 '22 17:10

mkopriva