How to ensure read-only variables and maps in golang - go

I want my programs have access to global read-only regex and maps. Here's what i thought of :
var myRegex *regexp.Regexp
var myMap map[string]string
func init() {
myRegex = regexp.MustCompile("blah")
myMap = map[string]string{"blah": "blah"}
}
or i can do
type myStruct struct {
// already have bunch of other struct fields
myRegex *regexp.Regexp
myMap map[string]string
}
func Initialize() *myStruct {
m := myStruct {
// bunch of other stuff
myRegex: regexp.MustCompile("blah")
myMap: map[string]string{"blah": "blah"}
}
But how i can ensure that these variables won't be writable by any other piece of my software ?

As long as you're dealing with pointers ( maps are pointers anyway ), you' ll never be able to ensure that your map or regex is ReadOnly.
( Ok, unless you copy the value with the function each time and return a new pointer... But i'm not sure that's what you want to achieve :)
If i take your example, and add a simple main code :
package main
import (
"fmt"
"regexp"
)
var myRegex *regexp.Regexp = regexp.MustCompile("blah")
var myMap map[string]string
func init() {
myRegex = regexp.MustCompile("blah")
myMap = map[string]string{"blah": "blah"}
}
type myStruct struct {
// already have bunch of other struct fields
myRegex *regexp.Regexp
myMap map[string]string
}
func Initialize() myStruct {
return myStruct{
// bunch of other stuff
myRegex: regexp.MustCompile("blah"),
myMap: map[string]string{"blah": "blah"},
}
}
func getMap() map[string]string {
return myMap
}
func main() {
fmt.Println(myMap)
myMap["blah2"] = "blah2"
fmt.Println(myMap)
fmt.Println(getMap())
m := Initialize()
fmt.Println(m.myMap)
m.myMap["test"] = "test"
fmt.Println(m.myMap)
}
you see that i can modify the maps:
❯ ./main
map[blah:blah]
map[blah:blah blah2:blah2]
map[blah:blah blah2:blah2]
map[blah:blah]
map[blah:blah test:test]
Regex would be exactly the same.
If you really want to ensure that your regex and maps will never be updated by mistake by another piece of code, there's couple of solutions; most of them consisting of moving your read-only variables in there own package and never give direct access to them. Something like this for instance
package mapreadonly
type ReadOnlyMap struct {
m map[string]string
}
func (elem ReadOnlyMap) Get(key string) (string, bool) {
value, ok := elem.m[key]
return value, ok
}
var Map1 ReadOnlyMap = ReadOnlyMap{
m: map[string]string{
"blah": "blah",
},
}
and then import this package in your other files that requires it.
But as said, your question is lacking a bit of context to ensure that the answer is what your expect.

Related

go: map of pointers or bools as map of interface

Suppose I have maps of different types, among them of bools and of pointers and want to pass them to some function in single way:
type Blah struct {
name string
}
var mapBlah map[string]*Blah = make(map[string]*Blah)
var mapBool map[string]bool = make(map[string]bool)
func joinKeys(m map[string]interface{}) string {
// returns strings.Join(keys of map)
}
I seemingly can't pass these maps into this function (example). How can this be done properly, or
why it can't be done if I'm missing something, please?
When you already know the types, I think switching over types will be beneficial like follows.
package main
import (
"fmt"
"strings"
)
type Blah struct {
name string
}
var mapBlah map[string]*Blah = make(map[string]*Blah)
var mapBool map[string]bool = make(map[string]bool)
func joinKeys(m interface{}) string {
var a []string
switch v := m.(type) {
case map[string]*Blah:
for k, _ := range v {
a = append(a, k)
}
case map[string]bool:
for k, _ := range v {
a = append(a, k)
}
default:
}
return strings.Join(a, ",")
}
func main() {
mapBlah["1B"] = &Blah{name: "first"}
mapBlah["2B"] = &Blah{name: "second"}
fmt.Println(joinKeys(mapBlah))
mapBool["1Bool"] = true
fmt.Println(joinKeys(mapBool))
}
For more dynamic way with some performance tradeoffs reflection is way to go.

Golang interface to struct

I have a function that has a parameter with the type interface{}, something like:
func LoadTemplate(templateData interface{}) {
In my case, templateData is a struct, but each time it has a different structure. I used the type "interface{}" because it allows me to send all kind of data.
I'm using this templateData to send the data to the template:
err := tmpl.ExecuteTemplate(w, baseTemplateName, templateData)
But now I want to append some new data and I don't know how to do it because the "interface" type doesn't allow me to add/append anything.
I tried to convert the interface to a struct, but I don't know how to append data to a struct with an unknown structure.
If I use the following function I can see the interface's data:
templateData = appendAssetsToTemplateData(templateData)
func appendAssetsToTemplateData(t interface{}) interface{} {
switch reflect.TypeOf(t).Kind() {
case reflect.Struct:
fmt.Println("struct")
s := reflect.ValueOf(t)
fmt.Println(s)
//create a new struct based on current interface data
}
return t
}
Any idea how can I append a child to the initial interface parameter (templateData)? Or how can I transform it to a struct or something else in order to append the new child/data?
Adrian is correct. To take it a step further, you can only do anything with interfaces if you know the type that implements that interface. The empty interface, interface{} isn't really an "anything" value like is commonly misunderstood; it is just an interface that is immediately satisfied by all types.
Therefore, you can only get values from it or create a new "interface" with added values by knowing the type satisfying the empty interface before and after the addition.
The closest you can come to doing what you want, given the static typing, is by embedding the before type in the after type, so that everything can still be accessed at the root of the after type. The following illustrates this.
https://play.golang.org/p/JdF7Uevlqp
package main
import (
"fmt"
)
type Before struct {
m map[string]string
}
type After struct {
Before
s []string
}
func contrivedAfter(b interface{}) interface{} {
return After{b.(Before), []string{"new value"}}
}
func main() {
b := Before{map[string]string{"some": "value"}}
a := contrivedAfter(b).(After)
fmt.Println(a.m)
fmt.Println(a.s)
}
Additionally, since the data you are passing to the template does not require you to specify the type, you could use an anonymous struct to accomplish something very similar.
https://play.golang.org/p/3KUfHULR84
package main
import (
"fmt"
)
type Before struct {
m map[string]string
}
func contrivedAfter(b interface{}) interface{} {
return struct{
Before
s []string
}{b.(Before), []string{"new value"}}
}
func main() {
b := Before{map[string]string{"some": "value"}}
a := contrivedAfter(b)
fmt.Println(a)
}
You can't append data arbitrarily to a struct; they're statically typed. You can only assign values to the fields defined for that specific struct type. Your best bet is probably to use a map instead of structs for this.
Not recommended, but you can create structs dynamically using the reflect package.
Here is an example:
package main
import (
"encoding/json"
"os"
"reflect"
)
type S struct {
Name string
}
type D struct {
Pants bool
}
func main() {
a := Combine(&S{"Bob"}, &D{true})
json.NewEncoder(os.Stderr).Encode(a)
}
func Combine(v ...interface{}) interface{} {
f := make([]reflect.StructField, len(v))
for i, u := range v {
f[i].Type = reflect.TypeOf(u)
f[i].Anonymous = true
}
r := reflect.New(reflect.StructOf(f)).Elem()
for i, u := range v {
r.Field(i).Set(reflect.ValueOf(u))
}
return r.Addr().Interface()
}
You could use something like the Combine function above to shmush any number of structs together. Unfortunately, from the documentation:
StructOf currently does not generate wrapper methods for embedded fields. This limitation may be lifted in a future version.
So your created struct won't inherit methods from the embedded types. Still, maybe it does what you need.
If you are just looking to convert your interface to struct, use this method.
type Customer struct {
Name string `json:"name"`
}
func main() {
// create a customer, add it to DTO object and marshal it
receivedData := somefunc() //returns interface
//Attempt to unmarshall our customer
receivedCustomer := getCustomerFromDTO(receivedData)
fmt.Println(receivedCustomer)
}
func getCustomerFromDTO(data interface{}) Customer {
m := data.(map[string]interface{})
customer := Customer{}
if name, ok := m["name"].(string); ok {
customer.Name = name
}
return customer
}

Collections of generic structs with embedded locks in golang

Below I have an example of one structure which embeds another. I'm trying to figure out how to pass the more specific structure pointer to be stored in a less specific one. You can think of it as a collection. Wrapping in an interface doesn't seem to work, as doing so would make a copy, which isn't valid for structs with locks. Ideas?
package stackoverflow
import "sync"
type CoolerThingWithLock struct {
fancyStuff string
ThingWithLock
}
func NewCoolerThingWithLock() *CoolerThingWithLock {
coolerThingWithLock := &CoolerThingWithLock{}
coolerThingWithLock.InitThingWithLock()
return coolerThingWithLock
}
type ThingWithLock struct {
value int
lock sync.Mutex
children []*ThingWithLock
}
func (thingWithLock *ThingWithLock) InitThingWithLock() {
thingWithLock.children = make([]*ThingWithLock, 0)
}
func NewThingWithLock() *ThingWithLock {
newThingWithLock := &ThingWithLock{}
newThingWithLock.InitThingWithLock()
return newThingWithLock
}
func (thingWithLock *ThingWithLock) AddChild(newChild *ThingWithLock) {
thingWithLock.children = append(thingWithLock.children, newChild)
}
func (thingWithLock *ThingWithLock) SetValue(newValue int) {
thingWithLock.lock.Lock()
defer thingWithLock.lock.Unlock()
thingWithLock.value = newValue
for _, child := range thingWithLock.children {
child.SetValue(newValue)
}
}
func main() {
thingOne := NewThingWithLock()
thingTwo := NewCoolerThingWithLock()
thingOne.AddChild(thingTwo)
thingOne.SetValue(42)
}
Error: cannot use thingTwo (type *CoolerThingWithLock) as type
*ThingWithLock in argument to thingOne.AddChild
It's impossible to store the wrapping type in []*ThignWithLock since go has no notion of structural subtyping.
Your assertion that an interface will result in copying is incorrect, and you can get the desired effect by doing:
type InterfaceOfThingThatParticipatesInAHierarchy interface {
AddChild(InterfaceOfThingThatParticipatesInAHierarchy)
SetValue(int)
}
type ThingWithLock struct {
...
children []InterfaceOfThingThatParticipatesInAHierarchy
}
func (thingWithLock *ThingWithLock) AddChild(newChild InterfaceOfThingThatParticipatesInAHierarchy) { ... }
As long as the interface is implemented on a *ThingWithLock and not ThingWithLock, there will be no copying of the receiver struct itself, only the pointer to the struct will be copied on the stack.

Adding to an anonymous slice in a type in golang

I want to add a few helper methods attached onto a slice.
So I created a type which is of []*MyType
Is there any way to add to that slice of MyTypes? append will not recognise the slice.
package main
import "fmt"
type MyType struct{
Name string
Something string
}
type MyTypes []*MyType
func NewMyTypes(myTypes ...*MyType)*MyTypes{
var s MyTypes = myTypes
return &s
}
//example of a method I want to be able to add to a slice
func(m MyTypes) Key() string{
var result string
for _,i := range m{
result += i.Name + ":"
}
return result
}
func main() {
mytype1 ,mytype2 := MyType{Name:"Joe", Something: "Foo"}, MyType{Name:"PeggySue", Something: "Bar"}
myTypes:= NewMyTypes(&mytype1,&mytype2)
//cant use it as a slice sadface
//myTypes = append(myTypes,&MyType{Name:"Random", Something: "asdhf"})
fmt.Println(myTypes.Key())
}
I don't want to wrap it in another type and name the param even though I'm sorta doing it.. Because of json marshalling will probably be different
What would be the way to add to the MyTypes slice?
I really want to just be able to add a method to a slice so it can implement a specific interface and not effect the marshalling.. Is there a different better way?
Thanks
Update: This answer once contained two ways to solve the problem: my somewhat clunky way, the DaveC's more elegant way. Here's his more elegant way:
package main
import (
"fmt"
"strings"
)
type MyType struct {
Name string
Something string
}
type MyTypes []*MyType
func NewMyTypes(myTypes ...*MyType) MyTypes {
return myTypes
}
//example of a method I want to be able to add to a slice
func (m MyTypes) Names() []string {
names := make([]string, 0, len(m))
for _, v := range m {
names = append(names, v.Name)
}
return names
}
func main() {
mytype1, mytype2 := MyType{Name: "Joe", Something: "Foo"}, MyType{Name: "PeggySue", Something: "Bar"}
myTypes := NewMyTypes(&mytype1, &mytype2)
myTypes = append(myTypes, &MyType{Name: "Random", Something: "asdhf"})
fmt.Println(strings.Join(myTypes.Names(), ":"))
}
Playground: https://play.golang.org/p/FxsUo1vu6L

Go: Am I creating too many values?

If I have a struct like this
type myStruct struct {
mystring string
myint int
}
and if I have a function that returns a new myStruct like this
func New() myStruct {
s := myStruct{}
s.mystring = "string"
s.myint = 1
return s
}
Because I first store it in the "s" variable before returning it, is my function actually making 2 myStruct values instead of one?
And if so, is it then a better practice to make sure I don't first store it in the variable?
The return statement will return a copy of the myStruct object value. If it is a small object then this is fine.
If you intend for the caller to be able to modify this object, and the struct will have methods that use a pointer as the receiver, then it makes more sense to return a pointer to your struct instead:
func New() *myStruct {
s := myStruct{}
s.mystring = "string"
s.myint = 1
return &s
}
You can see the copy happening when you compare the memory address of value vs pointer return types: http://play.golang.org/p/sj6mivYSHg
package main
import (
"fmt"
)
type myStruct struct {
mystring string
myint int
}
func NewObj() myStruct {
s := myStruct{}
fmt.Printf("%p\n", &s)
return s
}
func NewPtr() *myStruct {
s := &myStruct{}
fmt.Printf("%p\n",s)
return s
}
func main() {
o := NewObj()
fmt.Printf("%p\n",&o)
p := NewPtr()
fmt.Printf("%p\n",p)
}
0xf8400235a0 // obj inside NewObj()
0xf840023580 // obj returned to caller
0xf840023640 // ptr inside of NewPtr()
0xf840023640 // ptr returned to caller
I'm definitely not a Go expert (or even novice :) ), but as #max.haredoom mentioned, you can allocate variables in the function signature itself. In that way, you can also omit the s in the return:
package main
import "fmt"
type myStruct struct {
mystring string
myint int
}
func New() (s myStruct) {
s.mystring = "string"
s.myint = 1
return
}
func main() {
r := New()
fmt.Println(r)
}
// Outputs {string 1}
In the examples that I have come across in Effective Go, it does seem to be the most common way of doing things of this nature, but again, I am definitely not an authority on the subject (and will look for additional info on the actual performance).
I think I found the answer by using defer.
I updated the function so that there's a deferred modification to the myStruct value. This means it will happen after the return, but before it is received on the other end.
When I do this, the struct that is received by the caller does not show the updated value, so it appears as though I am indeed returning a copy.
func New() myStruct {
s := myStruct{}
defer func() {
s.mystring = "new value" // defer an update to the value
}()
s.mystring = "string"
s.myint = 1
return s
}
func main() {
b := New()
fmt.Println(b) // still shows the original value
}
http://play.golang.org/p/WWQi8HpDny

Resources