Questions tagged [go]

Go is an open-source programming language, with a syntax loosely derived from C. It's statically typed, with limited dynamic typing capabilities; it also features automatic memory management, built-in concurrency primitives, variable-length arrays – called slices –, and a large standard library.

Go (sometimes "Golang" for search-ability) is a general-purpose programming language. While originally created by Google, Go is an open source project with a large contributor base. It aims to be efficient both for development and execution with a focus on fast compilation and increased maintainability of large projects. Go was originally targeted at systems programming tasks such as building server/web applications, high throughput middleware, and databases, but it has a growing ecosystem of libraries allowing it to be used for a wide variety of tasks such as developing end-user daemons, CLIs, and desktop/mobile applications.

The first class concurrency mechanisms of Go make it easier to write programs that get the most out of multicore and networked machines, while its structural type system enables flexible and modular program construction. Go compiles quickly to memory safe machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that develops like a dynamically typed, interpreted language, but performs like native code.

Go Reference Documentation

Go Tutorials

Go Books (Paid)

Go Books (Free)

Popular Go Projects

Go Mailing Lists

Go chat

Online Go Compilers

Go FAQ

Go Code Editors & IDEs

Go Dependency Management

Go2 Resources

Go2 is an umbrella term used to indicate language changes under discussion that may break Go's compatibility promise. Even if a feature ends up being suited for a Go 1.x release, it is still often tagged and referred to as Go2. This includes proposals, draft specifications, dev implementations, etc.

70688 questions
228
votes
4 answers

How can I install a package with go get?

I want to install packages from github to my $GOPATH, I have tried this: go get github.com:capotej/groupcache-db-experiment.git the repository is here.
roger
  • 9,063
  • 20
  • 72
  • 119
226
votes
3 answers

How to join a slice of strings into a single string?

package main import ( "fmt" "strings" ) func main() { reg := [...]string {"a","b","c"} fmt.Println(strings.Join(reg,",")) } gives me an error of: prog.go:10: cannot use reg (type [3]string) as type []string in argument to strings.Join Is there…
cycle4passion
  • 3,081
  • 3
  • 14
  • 28
220
votes
11 answers

How to import local packages in go?

I am new to go and working on an example code that I want to localize. In the original main.go import statement it was: import ( "log" "net/http" "github.com/foo/bar/myapp/common" "github.com/foo/bar/myapp/routers" ) Now I have…
Karlom
  • 13,323
  • 27
  • 72
  • 116
220
votes
4 answers

Lowercase JSON key names with JSON Marshal in Go

I wish to use the "encoding/json" package to marshal a struct declared in one of the imported packages of my application. Eg.: type T struct { Foo int } Because it is imported, all available (exported) fields in the struct begins with an upper…
ANisus
  • 74,460
  • 29
  • 162
  • 158
219
votes
4 answers

Access HTTP response as string in Go

I'd like to parse the response of a web request, but I'm getting trouble accessing it as string. func main() { resp, err := http.Get("http://google.hu/") if err != nil { // handle error } defer resp.Body.Close() body,…
Tibor Szasz
  • 3,028
  • 2
  • 18
  • 23
217
votes
8 answers

How to trim leading and trailing white spaces of a string?

Which is the effective way to trim the leading and trailing white spaces of string variable in Go?
Alex Mathew
  • 3,925
  • 5
  • 21
  • 25
216
votes
9 answers

How can I "go run" a project with multiple files in the main package?

I have a single file in the main package called main.go. Because the code isn't reusable I want to separate part of the code in a different file but in the same package. How do I split the contents of main.go into multiple files without creating a…
Neil
  • 8,925
  • 10
  • 44
  • 49
216
votes
10 answers

How to import local packages without gopath

I've used GOPATH but for this current issue I'm facing it does not help. I want to be able to create packages that are specific to a project: myproject/ ├── binary1.go ├── binary2.go ├── package1.go └── package2.go I tried multiple ways but how do…
user1529891
215
votes
10 answers

How to pad a number with zeros when printing?

How can I print a number or make a string with zero padding to make it fixed width? For instance, if I have the number 12 and I want to make it 000012.
Travis Reeder
  • 38,611
  • 12
  • 87
  • 87
214
votes
6 answers

nil detection in Go

I see a lot of code in Go to detect nil, like this: if err != nil { // handle the error } however, I have a struct like this: type Config struct { host string port float64 } and config is an instance of Config, when I do: if…
user1663023
213
votes
5 answers

Convert a float64 to an int in Go

How does one convert a float64 to an int in Go? I know the strconv package can be used to convert anything to or from a string, but not between data types where one isn't a string. I know I can use fmt.Sprintf to convert anything to a string, and…
Chris Bunch
  • 87,773
  • 37
  • 126
  • 127
213
votes
7 answers

How to parse unix timestamp to time.Time

I'm trying to parse an Unix timestamp but I get out of range error. That doesn't really makes sense to me, because the layout is correct (as in the Go docs): package main import "fmt" import "time" func main() { tm, err :=…
hey
  • 7,299
  • 14
  • 39
  • 57
213
votes
6 answers

How to do a https request with bad certificate?

Say I want to get https://golang.org programatically. Currently golang.org (ssl) has a bad certificate which is issued to *.appspot.com So when I run this: package main import ( "log" "net/http" ) func main() { _, err :=…
topskip
  • 16,207
  • 15
  • 67
  • 99
212
votes
6 answers

How to format current time using a yyyyMMddHHmmss format?

I'm trying to format the current time using this format yyyyMMddHHmmss. t := time.Now() fmt.Println(t.Format("yyyyMMddHHmmss")) That is outputting: yyyyMMddHHmmss Any suggestions?
sergserg
  • 21,716
  • 41
  • 129
  • 182
212
votes
12 answers

How to use C++ in Go

In the new Go language, how do I call C++ code? In other words, how can I wrap my C++ classes and use them in Go?
Frank
  • 64,140
  • 93
  • 237
  • 324