How to validate UUID v4 in Go?

Regex is expensive. The following approach is ~18x times faster than the regex version. Use something like https://godoc.org/github.com/google/uuid#Parse instead. import “github.com/google/uuid” func IsValidUUID(u string) bool { _, err := uuid.Parse(u) return err == nil }

How check if a property was set in a struct

Like dyoo said, you can use nil if your struct properties are pointers. If you want to keep them as strings you can compare with “”. Here is a sample: package main import “fmt” type MyStruct struct { Property string } func main() { s1 := MyStruct{ Property: “hey”, } s2 := MyStruct{} if s1.Property … Read more

is there a way to iterate over constant used as enum

There is no direct way to enumerate the values/instances of named type at runtime, whether variables or constants, unless you specifically define a slice that lists them. This is left up to the definer or the user of the enumeration type. package main import ( “fmt” “time” ) var Weekdays = []time.Weekday{ time.Sunday, time.Monday, time.Tuesday, … Read more

Why do the Go-generated protobuf files contain mutex locks?

The impl.MessageState is embedded in concrete messages only, not in the generated structs that implement a proto message. It specifically embeds the three pragmas: NoUnkeyedLiterals, DoNotCompare, and DoNotCopy. The last one, DoNotCopy is a zero-sized array of sync.Mutex. The sole purpose is to have go vet complain loudly about shallow copies, as described in the … Read more

Go map with user-defined key with user-defined equality?

Go has strict comparable semantics for values used as map keys. As such, you cannot define your own hash code and equality functions for map keys as you can in many other languages. However, consider the following workaround. Instead of using the struct instances directly as a keys, use a derived attribute of the struct … Read more

How to structure Go application to produce multiple binaries?

The following folder structure is used in many popular go projects such as helm,kubicorn etc. goproject/ ├── bin ├── cmd ├── pkg └── Makefile The cmd directory will contain the different binaries, separated in directories. cmd/ ├── bin1 │   └── main.go ├── bin2 │   └── main.go └── bin3 └── main.go pkg directory will contain all … Read more

Checking equality of interface{}

Thanks to @CodingPickle comment, I provide the following from the Go Programming Language Specification The equality operators == and != apply to operands that are comparable. Regarding interface{}s and structs: Interface values are comparable. Two interface values are equal if they have identical dynamic types and equal dynamic values or if both have value nil. … Read more

Generic Structs with Go

Starting with Go 1.18, you can define generic types: type Model[T any] struct { Data []T } A generic type must be instantiated1 when used, and instantiation requires a type parameter list: func main() { // passing int as type parameter modelInt := Model[int]{Data: []int{1, 2, 3}} fmt.Println(modelInt.Data) // [1 2 3] // passing string … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)