Posts

Showing posts with the label Go

Create Array Of Array Literal In Golang

Answer : You almost have the right thing however your syntax for the inner arrays is slightly off, needing curly braces like; test := [][]int{[]int{1,2,3},[]int{1,2,3}} or a slightly more concise version; test := [][]int{{1,2,3},{1,2,3}} The expression is called a 'composite literal' and you can read more about them here; https://golang.org/ref/spec#Composite_literals But as a basic rule of thumb, if you have nested structures, you have to use the syntax recursively. It's very verbose. In some other langauges (Perl, Python, JavaScript), [1,2,3] might be an array literal, but in Go, composite literals use braces, and here, you have to specify the type of the outer slice: package main import "fmt" type T struct{ foo [][]int } func main() { a := [][]int{{1, 2, 3}, {4, 5, 6}} b := T{foo: [][]int{{1, 2, 3}, {4, 5, 6}}} fmt.Println(a, b) } You can run or play with that on the Playground. The Go compiler is just tricky enough to figure out that the eleme...

Convert Int Array To String Separated By ','

Answer : Make IDs a []string and convert the integers when you append them var IDs []string for _, i := range []int{1, 2, 3, 4} { IDs = append(IDs, strconv.Itoa(i)) } fmt.Println(strings.Join(IDs, ", ")) https://play.golang.org/p/xrfuMRjgiI I would prefer to use json.Marshal . It is much simple and easy to use. data := []int{100, 200, 300} s, _ := json.Marshal(data) fmt.Println(strings.Trim(string(s), "[]")) GoPlaygroundLink I hope this helps you. Please feel free to ask in case of doubts. WebsiteLink

Can I Import 3rd Party Package Into Golang Playground

Answer : Since May 14th, 2019, it is now possible (from Brad Fitzpatrick)! The #golang playground now supports third-party imports, pulling them in via https://proxy.golang.org/ Example: https://play.golang.org/p/eqEo7mqdS9l Multi-file support & few other things up next. Report bugs at golang/go issue 31944, or here on the tweeters. (On the "multiple file" support , see, since May. 16th 2019, "Which packages may be imported in the go playground?": see an example here) netbrain suggests in the comments another example: On the playground: package main import ( "fmt" "gonum.org/v1/gonum/mat" ) func main() { v1 := mat.NewVecDense(4,[]float64{1,2,3,4}) fmt.Println(mat.Dot(v1,v1)) } woud give '30', using mat.NewVecDense() to create a column vector, and mat.Dot() to return the sum of the element-wise product of v1 and v1 The point being: gonum/mat is not part of the Go Standard Library. Original ans...

Cannot Download, $GOPATH Not Set

Answer : [Update: as of Go 1.8, GOPATH defaults to $HOME/go , but you may still find this useful if you want to understand the GOPATH layout, customize it, etc.] The official Go site discusses GOPATH and how to lay out a workspace directory. export GOPATH="$HOME/your-workspace-dir/" -- run it in your shell, then add it to ~/.bashrc or equivalent so it will be set for you in the future. Go will install packages under src/ , bin/ , and pkg/ , subdirectories there. You'll want to put your own packages somewhere under $GOPATH/src , like $GOPATH/src/github.com/myusername/ if you want to publish to GitHub. You'll also probably want export PATH=$PATH:$GOPATH/bin in your .bashrc so you can run compiled programs under $GOPATH . Optionally, via Rob Pike, you can also set CDPATH so it's faster to cd to package dirs in bash: export CDPATH=.:$GOPATH/src/github.com:$GOPATH/src/golang.org/x means you can just type cd net/html instead of cd $GOPATH/src/golang....

Correct Approach To Global Logging In Golang

Answer : Create a single log.Logger and pass it around? That is possible. A log.Logger can be used concurrently from multiple goroutines. Pass around a pointer to that log.Logger? log.New returns a *Logger which is usually an indication that you should pass the object around as a pointer. Passing it as value would create a copy of the struct (i.e. a copy of the Logger) and then multiple goroutines might write to the same io.Writer concurrently. That might be a serious problem, depending on the implementation of the writer. Should each goroutine or function create a logger? I wouldn't create a separate logger for each function or goroutine. Goroutines (and functions) are used for very lightweight tasks that will not justify the maintenance of a separate logger. It's probably a good idea to create a logger for each bigger component of your project. For example, if your project uses a SMTP service for sending mails, creating a separate logger for the mail ser...