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
29
votes
3 answers

How to check if an object has a particular method?

In Go, how do you check if an object responds to a method? For example, in Objective-C this can be achieved by doing: if ([obj respondsToSelector:@selector(methodName:)]) { // if method exists [obj methodName:42]; // call the method }
nishanthshanmugham
  • 2,967
  • 1
  • 25
  • 29
29
votes
2 answers

Initialize an array of structs inside a nested struct in golang

I am wondering how can I define and initialize and array of structs inside a nested struct, for example: type State struct { id string `json:"id" bson:"id"` Cities } type City struct { id string `json:"id" bson:"id"` } type Cities…
mquemazz
  • 763
  • 3
  • 12
  • 18
29
votes
4 answers

Does go garbage collect parts of slices?

If I implement a queue like this... package main import( "fmt" ) func PopFront(q *[]string) string { r := (*q)[0] *q = (*q)[1:len(*q)] return r } func PushBack(q *[]string, a string) { *q = append(*q, a) } func main() { q…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
29
votes
1 answer

How to unmarshal JSON into interface{} in Go?

I'm a newbie in Go and now I have a problem. I have a type called Message, it is a struct like this: type Message struct { Cmd string `json:"cmd"` Data interface{} `json:"data"` } I also have a type called CreateMessage like this: type…
Papulatus
  • 677
  • 2
  • 8
  • 18
29
votes
2 answers

How to compare [32]byte with []byte?

I want to compare output of sha256.Sum256() which is [32]byte with a []byte. I am getting an error "mismatched types [32]byte and []byte". I am not able to convert []byte to [32]byte. Is there a way to do this?
Sumit Rathore
  • 571
  • 2
  • 7
  • 19
29
votes
3 answers

Go install doesn't create any bin file

My folder structure is correct, i can both run go install from inside the package folder and from anywhere in the system, adding the package (folder) name after install. For example, my workspace is the following: Go\ bin\ pkg\ src\ name\ …
castan
  • 383
  • 1
  • 4
  • 11
29
votes
3 answers

Best practice to maintain a mgo session

I'm currently using a mongodb with mgo lib for a web application, but I'm not sure if the way I'm using it, is good one .. package db import ( "gopkg.in/mgo.v2" ) const ( MongoServerAddr = "192.168.0.104" RedisServerAddr =…
JonathanChaput
  • 334
  • 1
  • 4
  • 9
29
votes
2 answers

OK to exit program with active goroutine?

Take the following code snippet: func main() { ch := make(chan int) quit := make(chan int) go func() { for { ch <- querySomePeriodicThing() } }() // ... loop: for { select { case…
josmith42
  • 848
  • 2
  • 10
  • 18
29
votes
1 answer

What does ^ do?

I hope this question is not too stupid... I have no idea what the ^ operator does in Go, e.g. a := 3^500 At first I thought it must be pow but it most certainly is not. It's not mod (%) either. I've tried looking through the doc and searching on…
Alasdair
  • 13,348
  • 18
  • 82
  • 138
29
votes
2 answers

How would you set and clear a single bit in Go?

In Golang, how do you set and clear individual bits of an integer? For example, functions that behave like this: clearBit(129, 7) // returns 1 setBit(1, 7) // returns 129
Kevin Burke
  • 61,194
  • 76
  • 188
  • 305
29
votes
2 answers

Defining a function that returns a slice of variable size in golang

I would like to build a function that returns a slice of any size. I know I can do func BuildSlice() [100]int { return [100]int{} } but I would like to be able to return slices of different sizes from the same function. Something like: func…
Daniel Nill
  • 5,539
  • 10
  • 45
  • 63
29
votes
4 answers

golang - ceil function like php?

I want to return the least integer value greater than or equal to integer division. So I used math.ceil, but can not get the value I want. package main import ( "fmt" "math" ) func main() { var pagesize int = 10 var length int =…
leiyonglin
  • 6,474
  • 12
  • 36
  • 41
29
votes
6 answers

Does Go have no real way to shrink a slice? Is that an issue?

I've been trying out Go for some time and this question keeps bugging me. Say I build up a somewhat large dataset in a slice (say, 10 million int64s). package main import ( "math" "fmt" ) func main() { var a []int64 var i int64; …
justinas
  • 6,287
  • 3
  • 26
  • 36
29
votes
3 answers

How expensive is []byte(string)?

Let's convert string to []byte: func toBytes(s string) []byte { return []byte(s) // What happens here? } How expensive is this cast operation? Is copying performed? As far as I see in Go specification: Strings behave like slices of bytes but are…
demi
  • 5,384
  • 6
  • 37
  • 57
29
votes
7 answers

git library for Go

As a pet project, I want to develop a note taking app using git as storage backend. (I suspect this doesn't exist yet, given this guy's blog post: http://jarofgreen.co.uk/2012/08/how-about-a-mobile-note-app-backed-by-git/ ) Now, I'd like to take…
vhdirk
  • 598
  • 1
  • 4
  • 12
1 2 3
99
100