GithubHelp home page GithubHelp logo

gen-table-structure's Introduction

GenStructByTable

​ GenStructByTable是一个简单的代码生成工具,具体工作是讲mysql中的表生成成go中的结构体。以下是简单的demo:

在生成代码之前你可能需要配置一个简单的template文件,以此来代表你需要的生成的代码的模版,详情可以参考golang text/template的语法。

package {{.PackageName}}


type {{.StructName}} struct {
    {{- range $i, $v := .Meta }}
        {{$v.CamelName}} {{$v.DataTypeInGo}}
    {{- end }}
}

写好template之后可以参考以下代码生成代码:

func TestGenerator_Gen(t *testing.T) {
	config := &Config{
		Host:     "127.0.0.1",
		Port:     3306,
		Username: "root",
		Password: "", // 我才不告诉你我的密码呢。
	}
	g, err := NewGenerator(config)
	if err != nil {
		t.Errorf("have err during NewGenerator, err is %s", err)
		return
	}
	genInfo := &GenInfo{
		Schema:       "elliot_test",
		Table:        "test_table",
		ExportFolder: "",
		TemplatePath: "struct_gen_test_template",
		FileName:     "test_gen.go",
		PackageName:  "table_gen",
		StructName:   "TestGenStruct",
	}
	isSuccess, err := g.Gen(genInfo)
	if err != nil {
		t.Errorf("have err during Gen file, err is %s", err)
		return
	}
	t.Logf("does it gen file successully?  %v", isSuccess)
}

gen-table-structure's People

Contributors

elliotchenzichang avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

xiantang

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.