How to find the type of an object in Go?

The Go reflection package has methods for inspecting the type of variables. The following snippet will print out the reflection type of a string, integer and float. package main import ( “fmt” “reflect” ) func main() { tst := “string” tst2 := 10 tst3 := 1.2 fmt.Println(reflect.TypeOf(tst)) fmt.Println(reflect.TypeOf(tst2)) fmt.Println(reflect.TypeOf(tst3)) } Output: Hello, playground string int … Read more

How can I convert a zero-terminated byte array to string?

Methods that read data into byte slices return the number of bytes read. You should save that number and then use it to create your string. If n is the number of bytes read, your code would look like this: s := string(byteArray[:n]) To convert the full string, this can be used: s := string(byteArray[:len(byteArray)]) … Read more

How to check if a file exists in Go?

To check if a file doesn’t exist, equivalent to Python’s if not os.path.exists(filename): if _, err := os.Stat(“/path/to/whatever”); errors.Is(err, os.ErrNotExist) { // path/to/whatever does not exist } To check if a file exists, equivalent to Python’s if os.path.exists(filename): Edited: per recent comments if _, err := os.Stat(“/path/to/whatever”); err == nil { // path/to/whatever exists } … Read more

How to print struct variables in console?

To print the name of the fields in a struct: fmt.Printf(“%+v\n”, yourProject) From the fmt package: when printing structs, the plus flag (%+v) adds field names That supposes you have an instance of Project (in ‘yourProject‘) The article JSON and Go will give more details on how to retrieve the values from a JSON struct. … Read more

Optional Parameters in Go?

Go does not have optional parameters nor does it support method overloading: Method dispatch is simplified if it doesn’t need to do type matching as well. Experience with other languages told us that having a variety of methods with the same name but different signatures was occasionally useful but that it could also be confusing … Read more

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