Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8826477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:08:52+00:00 2026-06-14T07:08:52+00:00

In object oriented languages I use class variables to track how many instances are

  • 0

In object oriented languages I use class variables to track how many instances are currently spawned by incrementing on construction and decrementing on destruction.

I try to implement similar behaviour in go:

package entity

type Entity struct {
    Name string
}

func New(name string) Entity {
    entity := Entity{name}
    counter++
    return entity
}

var counter int = 0

func (e *Entity) Count() int {
    return counter
}

and that works half way as I can not decrement the counter via a destructor.

Can I somehow mimic object destruction?
How would I keep track of instance count correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:08:53+00:00Added an answer on June 14, 2026 at 7:08 am

    You can use runtime.SetFinalizer like this. See here for playground version.

    package main
    
    import (
        "fmt"
        "runtime"
    )
    
    type Entity struct {
        Name string
    }
    
    var counter int = 0
    
    func New(name string) Entity {
        entity := Entity{name}
        counter++
        runtime.SetFinalizer(&entity, func(_ *Entity) {
            counter--
        })
        return entity
    }
    
    func (e *Entity) Count() int {
        return counter
    }
    
    func main() {
        e := New("Sausage")
        fmt.Println("Entities", counter, e)
        e = New("Potato")
        fmt.Println("Entities", counter, e)
        runtime.GC()
        fmt.Println("Entities", counter)
        e = New("Leek")
        fmt.Println("Entities", counter)
        runtime.GC()
        fmt.Println("Entities", counter)
    }
    

    This prints

    Entities 1 {Sausage}
    Entities 2 {Potato}
    Entities 0
    Entities 1
    Entities 0
    

    Note this from the docs for gotchas with Finalizers

    The finalizer for x is scheduled to run at some arbitrary time after x
    becomes unreachable. There is no guarantee that finalizers will run
    before a program exits, so typically they are useful only for
    releasing non-memory resources associated with an object during a
    long-running program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading some slides of a class on object oriented programming languages and stepped
I'm familiar with object-oriented architecture, including use of design patterns and class diagrams for
There are quite a few dynamically typed object oriented languages in which a class
In most other Object Oriented languages. it would be sacrilege to have each function
Is the nesting of functions possible in the object oriented languages like C#, Java,
I'm planning on learning Java, I have experience programming in other Object Oriented languages,
For instance, I know that basically all languages that are object oriented based are
Wikipedia used to say* about duck-typing : In computer programming with object-oriented programming languages,
Programming languages like C# or Java feature static methods, despite being heavily object oriented.
When writing object-oriented software, I use dependency injection a lot: to compose together high-level

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.