V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
anc95

写了一个 Golang 的 enum 到 typescript 的转换器

  •  
  •   anc95 ·
    anc95 · Feb 27, 2022 · 2197 views
    This topic created in 1521 days ago, the information mentioned may be changed or developed.

    地址是这里: https://golang-enum-to-ts-playground.vercel.app ,大家可以随便玩一玩

    为什么写这个东东

    使用场景是这样子的,在日常的开发中,我们经常会用到一些枚举字段,枚举字段可以用来表示任务状态,或者是选项,或者一些简单的二元值,这些值在数据库里通常是以整形的方式存储

    在后端开发中,是使用具有语意话的变量来表示这些整形,如

    type Sex int
    const {
      Male Sex = iota
      Female
    }
    

    而传给前端的数据往往是,0 、1 这样没有语义化的值,所以前端通常也需要定义类似的枚举来方便代码编写

    enum Sex {
      Male,
      Female
    }
    

    也就是说前后端需要用不同语言维护一个相同意义的字段,来达到对于 0 、1 这样的数据的翻译

    所以写了这个东西,直接把后端的 enum 类型转换成 前端的 Typescript 的 enum ,而省掉这一层的手动转换

    4 replies    2022-02-28 10:38:24 +08:00
    freshgoose
        1
    freshgoose  
       Feb 27, 2022
    牛批,做这么个小工具,居然还用上了网页 IDE ,这就是狮子搏兔亦用全力吧
    anc95
        2
    anc95  
    OP
       Feb 27, 2022
    @freshgoose IDE 就用的微软的 monaco editor, 我就引用一下不费劲
    maocat
        3
    maocat  
       Feb 28, 2022 via iPhone
    我选择 protobuf
    anc95
        4
    anc95  
    OP
       Feb 28, 2022
    @maocat protobuf 是结合 grpc-web 用么
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2836 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 15:43 · PVG 23:43 · LAX 08:43 · JFK 11:43
    ♥ Do have faith in what you're doing.