How to assign string to bytes array
Safe and simple: []byte(“Here is a string….”)
Safe and simple: []byte(“Here is a string….”)
Sprintf is what you are looking for. Example fmt.Sprintf(“foo: %s”, bar) You can also see it in use in the Errors example as part of “A Tour of Go.” return fmt.Sprintf(“at %v, %s”, e.When, e.What)
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
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
A tag for a field allows you to attach meta-information to the field which can be acquired using reflection. Usually it is used to provide transformation info on how a struct field is encoded to or decoded from another format (or stored/retrieved from a database), but you can use it to store whatever meta-info you … Read more
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
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
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
Use the strconv package’s Itoa function. For example: package main import ( “strconv” “fmt” ) func main() { t := strconv.Itoa(123) fmt.Println(t) } You can concat strings simply by +‘ing them, or by using the Join function of the strings package.
Quoting from the language specs:Iota Within a constant declaration, the predeclared identifier iota represents successive untyped integer constants. It is reset to 0 whenever the reserved word const appears in the source and increments after each ConstSpec. It can be used to construct a set of related constants: const ( // iota is reset to … Read more