Go - 静态成员变量,例如OOP语言

Emi*_*m23 5 oop go

我是Go的新手,然后我搜索了很多如何使用静态函数/变量(例如C#)的静态类.但是,我找不到任何有关它的答案.也许这个问题看起来很愚蠢,但是当我不确定或者什么时候我完全不理解时,我也不喜欢这个问题.

假设我们有这个代码:

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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我理解Golang是一个类似C的,那么它是否有一些像上面这样的行为,比如C++/C#语言?我上面的代码可以用C++/C#来实现,或者它的方法是通过C语言传递(使用C模块化编程方式)?

小智 16

Go中没有继承,
但你可以用Golang方式做所有OOP的东西.

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

1:C#中的static var => Golang包中的全局变量
2:C#中的枚举=>具有枚举名称的新包和枚举元素的常量类型
3:OOP中的类=>结构类型
4:类方法=>带接收器的结构方法
5:C#/ Java抽象方法(C++中的纯虚函数)=>接口方法如io.Reader
6:public => first letter大写名称
7:private =>第一个字母小写名称
8:namespace =>包名
9:inheritance =>嵌入式结构和嵌入式接口
10:Thread => Go例程
11:lock => sync.Mutex
...