10

I'm pretty new with Go and then I searched a lot how to have a static class with static function/variables such as C# by example. But, I couldn't find anything which answered well about it. Maybe this question seems stupid, but I don't like either when I'm not sure or when I don't understand completely something.

Let say we have this code:

public class Program
{
    public static string name = "Program tester.";

    public enum Importance
    {
        None,
        Trivial,
        Regular,
        Important,
        Critical
    };

    public static void tester(Importance value)
    {
        // ... Test against known Importance values.
        if (value == Importance.Trivial)
        {
            Console.WriteLine("Not true");
        }
        else if (value == Importance.Critical)
        {
            Console.WriteLine("True");
        }
    }
}

Golang is a C-like if I understand, so does it have some behavior like this one above, such as C++/C# languages? My code above can be achieved as C++/C# or the way to do it is to passing by a language as C (using the C modular programming way)?

6
  • 3
    Go doesn't have classes in the way C++, C# and Java do. It has simple aggregate-like user-defined types and separately defined free functions operating on those types. Constants like yours would probably be some kind of package-local constant in Go. Commented May 29, 2016 at 14:01
  • Thank for your answer :) So it means I can achieve it by declaring a struct and then, have a method linked to this one to initialize everything. Too used to use OOP ! ^^ Commented May 29, 2016 at 14:04
  • A function is not the same as a method (but a method is a function). Commented May 29, 2016 at 14:22
  • A method has to be linked to a class, isn't? Commented May 29, 2016 at 14:27
  • 2
    @MaximeGuittet in Go, a method is a function applied to a receiver (could be translated into a function with the receiver as its first argument): golang.org/ref/spec#Method_declarations Commented May 29, 2016 at 18:58

2 Answers 2

37

There is no inheritance in Go,
but you can do all OOP stuff in Golang way.

also see:
https://github.com/luciotato/golang-notes/blob/master/OOP.md https://www.goinggo.net/2013/07/object-oriented-programming-in-go.html

1: static var in C# class => global var in Golang package
2: enum in C# => new package with enum name and const type of enum elements
3: class in OOP => struct type
4: class methods => struct with receiver methods
5: C#/Java abstract methods(pure virtual functions in C++) => interface methods like io.Reader
6: public => first letter Upper case Name
7: private => first letter lower case name
8: namespace => package name
9: inheritance => embedded struct and embedded interface
10: Thread => Go routines
11: lock => sync.Mutex
...

Sign up to request clarification or add additional context in comments.

3 Comments

This answer is exactly what I wanted ! Thank !
this is, in fact, a very very useful way of looking go comming from an OO world like java... thanks! What about channels? I guess there is no direct isomorphism for that... but i don't know why, it recall me how we used to code the stdout in c++, for example std::cout << "hello" (instead of a <<, now they use an <-)
How is "there is no inheritance in Go" an answer to the question of "how to add a static member to a struct"?
0

There's no real way to get a static member of a struct like you would with a static member of a Java class, but you add metadata or a tag to a struct using StructTags:

https://medium.com/golangspec/tags-in-golang-3e5db0b8ef3e

What are the use(s) for tags in Go?

https://golang.org/pkg/reflect/#StructTag

typically we see them when creating a struct for use with unmarshaing JSON:

type Foo struct {
  Bar string `json:"bar,omitempty"`
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.