2

I have a structure:

type xyz struct {
    x int
    y string
}

func f(){
    x := new(xyz) //allocating memory
}

But I cannot find any method to deallocate it in go.

  • Is it not needed to deallocate it in golang?
  • Is there any useful document for how memory allocation/deallocation happens in go?
1
  • 2
    I think labeling new(xyz) with "allocating memory" is a dangerous assertion: There are far more ways in Go which result in an allocation. Even a:=2 might allocate memory on the heap if a escapes. Additionally: Using new like that is not idiomatic; using &xyz{} is more common. Commented Nov 21, 2014 at 13:47

1 Answer 1

16

Go is garbage collected language. You do not have to deallocate memory.

Articles on memory allocation and deallocation in Go.

  1. Garbage collection
  2. Heap and stack allocation
  3. Discussion on allocation optimization
  4. Variable allocation
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.