duanpin2034 2016-05-29 13:57
浏览 312
已采纳

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

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)?

  • 写回答

2条回答 默认 最新

  • doutan8601 2016-06-05 14:22
    关注

    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
    ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么