How to use String method of model Package

Best Mock code snippet using model.String

MenuModel_gen.go

Source:MenuModel_gen.go Github

copy

Full Screen

...217// GetLabel (get_label)218// Returns the label for the specified |command_id| or NULL if not found.219// The resulting string must be freed by calling cef_string_userfree_free().220func (d *MenuModel) GetLabel(commandID int32) string {221	return cefuserfreestrToString(C.gocef_menu_model_get_label(d.toNative(), C.int(commandID), d.get_label))222}223// GetLabelAt (get_label_at)224// Returns the label at the specified |index| or NULL if not found due to225// invalid range or the index being a separator.226// The resulting string must be freed by calling cef_string_userfree_free().227func (d *MenuModel) GetLabelAt(index int32) string {228	return cefuserfreestrToString(C.gocef_menu_model_get_label_at(d.toNative(), C.int(index), d.get_label_at))229}230// SetLabel (set_label)231// Sets the label for the specified |command_id|. Returns true (1) on success.232func (d *MenuModel) SetLabel(commandID int32, label string) int32 {233	label_ := C.cef_string_userfree_alloc()234	setCEFStr(label, label_)235	defer func() {236		C.cef_string_userfree_free(label_)237	}()238	return int32(C.gocef_menu_model_set_label(d.toNative(), C.int(commandID), (*C.cef_string_t)(label_), d.set_label))239}240// SetLabelAt (set_label_at)241// Set the label at the specified |index|. Returns true (1) on success.242func (d *MenuModel) SetLabelAt(index int32, label string) int32 {...

Full Screen

Full Screen

DataSampel.go

Source:DataSampel.go Github

copy

Full Screen

1package main2import (3	. "eaciit/secsampeldata/Datahelper"4	// "fmt"5	// . "github.com/ahmetalpbalkan/go-linq"6	"github.com/metakeule/fmtdate"7	// "github.com/tealeg/xlsx"8	// "gopkg.in/mgo.v2/bson"9	//"bytes"10	//"encoding/csv"11	// "io/ioutil"12	// "math"13	//"os"14	"math/rand"15	"strconv"16	// "strings"17	"time"18)19func main() {20	// sampelAssetClass()21	// sampelAssetLevel()22	// SampleLocation()23	// SampleAssetType()24	// SamplePlant()25	sampelAsset()26	// sampleAssetPlacements()27	// sampleAssetPerformance()28	// sampleAssetMaintenance()29	// sampleAssetFinancial()30	// sampleAssetMaintenanceSchedule()31}32type ModelAssetClass struct {33	Code      string34	ClassName string35}36type ModelAssetLevel struct {37	Code      string38	LevelName string39}40type ModelSampleLocation struct {41	Country   string42	City      string43	Longitude float6444	Latitude  float6445}46type ModelSampleAssetType struct {47	Code     string48	TypeName string49}50type ModelSamplePlant struct {51	Name     string52	Code     string53	Location ModelSampleLocation54}55func sampelAssetClass() {56	//fmt.Println("Coba")57	for i := 1; i <= 5; i++ {58		var model ModelAssetClass59		model.Code = "Class" + strconv.Itoa(i)60		model.ClassName = "Class " + strconv.Itoa(i)61		Save("SampleAssetClass", model)62	}63}64func sampelAssetLevel() {65	for i := 1; i <= 5; i++ {66		var model ModelAssetLevel67		model.Code = "Level" + strconv.Itoa(i)68		model.LevelName = "Level  " + strconv.Itoa(i)69		Save("SampleAssetLevel", model)70	}71}72func SampleLocation() {73	for i := 1; i <= 5; i++ {74		var model ModelSampleLocation75		model.Country = "Country" + strconv.Itoa(i)76		model.City = "City" + strconv.Itoa(i)77		model.Longitude = 078		model.Latitude = 079		Save("SampleLocation", model)80	}81}82func SampleAssetType() {83	for i := 1; i <= 5; i++ {84		var model ModelSampleAssetType85		model.Code = "Type" + strconv.Itoa(i)86		model.TypeName = "Type " + strconv.Itoa(i)87		Save("SampleAssetType", model)88	}89}90func SamplePlant() {91	var ResultLoc []ModelSampleLocation92	PopulateAsObject(&ResultLoc, "SampleLocation", nil, 0, 0)93	for i := 1; i <= 5; i++ {94		var model ModelSamplePlant95		model.Name = "Plant " + strconv.Itoa(i)96		model.Code = "Plant" + strconv.Itoa(i)97		model.Location = ResultLoc[rand.Intn(len(ResultLoc))]98		Save("SamplePlant", model)99	}100}101type ModelAsset struct {102	Code              string103	Name              string104	Manufacturer      string105	ManufacturedYear  int106	Class             ModelAssetClass107	Type              ModelSampleAssetType108	Level             ModelAssetLevel109	NormalCapacity    int110	Notes             string111	PurchaseDate      time.Time112	PurchaseCost      float64113	PurchaseCondition string114	PurchaseVendor    string115	PurchaseDiscount  int116	EstimatedUsage    int117	AmortizationRate  int118	DepresiationRate  int119	LatestStatus      string120}121func sampelAsset() {122	var (123		ResultClass []ModelAssetClass124		ResultType  []ModelSampleAssetType125		ResultLevel []ModelAssetLevel126		dateRand    string127	)128	ManufacturedYearVal := [2]int{2015, 2014}129	CapacityVal := [8]int{15, 17, 10, 15, 22, 12, 14, 20}130	Monthstring := [12]string{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}131	CostVal := [8]float64{1500, 1700, 1000, 1500, 2200, 1200, 1400, 2000}132	ConditionVal := [2]string{"New", "Used"}133	ManufacturerVal := [3]string{"eaciit", "pertamina", "telkom"}134	DiskonVal := [4]int{15, 20, 25, 50}135	StatusVal := [4]string{"Used", "Broken", "Available", "Other"}136	PopulateAsObject(&ResultClass, "SampleAssetClass", nil, 0, 0)137	PopulateAsObject(&ResultType, "SampleAssetType", nil, 0, 0)138	PopulateAsObject(&ResultLevel, "SampleAssetLevel", nil, 0, 0)139	for i := 1; i <= 100; i++ {140		var (141			model ModelAsset142		)143		model.Code = "Asset" + strconv.Itoa(i)144		model.Name = "Asset " + strconv.Itoa(i)145		model.Manufacturer = ManufacturerVal[rand.Intn(len(ManufacturerVal))]146		model.ManufacturedYear = ManufacturedYearVal[rand.Intn(len(ManufacturedYearVal))]147		model.Class = ResultClass[rand.Intn(len(ResultClass))]148		model.Type = ResultType[rand.Intn(len(ResultType))]149		model.Level = ResultLevel[rand.Intn(len(ResultLevel))]150		model.NormalCapacity = CapacityVal[rand.Intn(len(CapacityVal))]151		model.Notes = "Note " + strconv.Itoa(i)152		dateRand = strconv.Itoa(ManufacturedYearVal[rand.Intn(len(ManufacturedYearVal))]) + "/" + Monthstring[rand.Intn(len(Monthstring))] + "/" + strconv.Itoa(CapacityVal[rand.Intn(len(CapacityVal))]) + " 00:00:00"153		model.PurchaseDate, _ = fmtdate.Parse("YYYY/MM/DD hh:mm:ss", dateRand)154		model.PurchaseCost = CostVal[rand.Intn(len(CostVal))]155		model.PurchaseCondition = ConditionVal[rand.Intn(len(ConditionVal))]156		model.PurchaseVendor = "eaciit"157		model.PurchaseDiscount = DiskonVal[rand.Intn(len(DiskonVal))]158		// dateRand = strconv.Itoa(ManufacturedYearVal[rand.Intn(len(ManufacturedYearVal))]) + "/" + Monthstring[rand.Intn(len(Monthstring))] + "/" + strconv.Itoa(CapacityVal[rand.Intn(len(CapacityVal))]) + " 00:00:00"159		model.EstimatedUsage = rand.Intn(12 + 1)160		if model.EstimatedUsage == 0 {161			model.EstimatedUsage = 1162		}163		model.AmortizationRate = DiskonVal[rand.Intn(len(DiskonVal))]164		model.DepresiationRate = DiskonVal[rand.Intn(len(DiskonVal))]165		model.LatestStatus = StatusVal[rand.Intn(len(StatusVal))]166		// fmt.Println(model.PurchaseDate)167		Save("SampleAsset", model)168	}169}170type ModelAssetPlacements struct {171	Plants             ModelSamplePlant172	Assets             ModelAsset173	PlacedDate         time.Time174	PlacedCondition    string175	ReturnDate         time.Time176	ReturnCondition    string177	LatestAvailability int178}179func sampleAssetPlacements() {180	var (181		ResultPlant []ModelSamplePlant182		ResultAsset []ModelAsset183		dateRand    string184	)185	ManufacturedYearVal := [2]int{2015, 2014}186	Monthstring := [12]string{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}187	CapacityVal := [8]int{15, 17, 10, 15, 22, 12, 14, 20}188	ConditionVal := [2]string{"New", "Used"}189	PopulateAsObject(&ResultPlant, "SamplePlant", nil, 0, 0)190	PopulateAsObject(&ResultAsset, "SampleAsset", nil, 0, 0)191	for i := 1; i <= 100; i++ {192		var model ModelAssetPlacements193		model.Plants = ResultPlant[rand.Intn(len(ResultPlant))]194		model.Assets = ResultAsset[rand.Intn(len(ResultAsset))]195		dateRand = strconv.Itoa(ManufacturedYearVal[rand.Intn(len(ManufacturedYearVal))]) + "/" + Monthstring[rand.Intn(len(Monthstring))] + "/" + strconv.Itoa(CapacityVal[rand.Intn(len(CapacityVal))]) + " 00:00:00"196		model.PlacedDate, _ = fmtdate.Parse("YYYY/MM/DD hh:mm:ss", dateRand)197		model.PlacedCondition = ConditionVal[rand.Intn(len(ConditionVal))]198		dateRand = strconv.Itoa(ManufacturedYearVal[rand.Intn(len(ManufacturedYearVal))]) + "/" + Monthstring[rand.Intn(len(Monthstring))] + "/" + strconv.Itoa(CapacityVal[rand.Intn(len(CapacityVal))]) + " 00:00:00"199		model.ReturnDate, _ = fmtdate.Parse("YYYY/MM/DD hh:mm:ss", dateRand)200		model.ReturnCondition = ConditionVal[rand.Intn(len(ConditionVal))]201		model.LatestAvailability = CapacityVal[rand.Intn(len(CapacityVal))]202		Save("SampleAssetPlacements", model)203	}204}205type ModelAssetPerformance struct {206	Assets           ModelAsset207	StartsTime       time.Time208	StopTime         time.Time209	Duration         int210	Availability     int211	UtilizedDuration int212	UtilizedPower    int213}214func sampleAssetPerformance() {215	var (216		ResultAsset []ModelAsset217		dateRand    string218	)219	ManufacturedYearVal := [2]int{2015, 2014}220	Monthstring := [12]string{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}221	CapacityVal := [8]int{15, 17, 10, 15, 22, 12, 14, 20}222	hourRand := [5]time.Duration{3, 5, 6, 9, 8}223	PopulateAsObject(&ResultAsset, "SampleAsset", nil, 0, 0)224	for i := 1; i <= 100; i++ {225		var (226			model     ModelAssetPerformance227			startrand time.Time228			hourVal   time.Duration229		)230		model.Assets = ResultAsset[rand.Intn(len(ResultAsset))]231		dateRand = strconv.Itoa(ManufacturedYearVal[rand.Intn(len(ManufacturedYearVal))]) + "/" + Monthstring[rand.Intn(len(Monthstring))] + "/" + strconv.Itoa(CapacityVal[rand.Intn(len(CapacityVal))]) + " 00:00:00"232		// model.StartsTime, _ = fmtdate.Parse("YYYY/MM/DD hh:mm:ss", dateRand)233		startrand, _ = fmtdate.Parse("YYYY/MM/DD hh:mm:ss", dateRand)234		// dateRand = strconv.Itoa(ManufacturedYearVal[rand.Intn(len(ManufacturedYearVal))]) + "/" + Monthstring[rand.Intn(len(Monthstring))] + "/" + strconv.Itoa(CapacityVal[rand.Intn(len(CapacityVal))]+3) + " 00:00:00"235		hourVal = hourRand[rand.Intn(len(hourRand))]236		model.StartsTime = startrand.Add(hourVal * time.Hour)237		hourVal = hourRand[rand.Intn(len(hourRand))]238		model.StopTime = model.StartsTime.Add(hourVal * time.Hour)239		model.Duration = model.StopTime.Hour() - model.StartsTime.Hour()240		// fmt.Println(rand.Intn(5))241		model.Availability = CapacityVal[rand.Intn(len(CapacityVal))]242		model.UtilizedDuration = rand.Intn(model.Duration + 1)243		if model.UtilizedDuration == 0 {244			model.UtilizedDuration = 1245		}246		model.UtilizedPower = CapacityVal[rand.Intn(len(CapacityVal))]247		Save("SampleAssetPerformance", model)248	}249}250type ModelAssetMaintenance struct {251	Assets              ModelAsset252	StartsTime          time.Time253	StopTime            time.Time254	Duration            int255	MaintenanceStatus   string256	MaintenanceBy       string257	MaintenanceGroup    string258	ChangedParts        string259	CausedBy            string260	ConditionBefore     string261	ConditionAfter      string262	ConditionPercentage int263	Notes               string264}265func sampleAssetMaintenance() {266	var (267		ResultAsset []ModelAsset268		dateRand    string269	)270	ManufacturedYearVal := [2]int{2015, 2014}271	Monthstring := [12]string{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}272	CapacityVal := [8]int{15, 17, 10, 15, 22, 12, 14, 20}273	StatusVal := [2]string{"Scheduled", "UnPlanned"}274	PersenVal := [7]int{15, 20, 25, 50, 70, 75, 80}275	hourRand := [5]time.Duration{3, 5, 6, 9, 8}276	GroupVal := [6]string{"Failure", "Renewal Overhauls", "Risk Limited", "Operating Time", "Condition Based", "Others"}277	PopulateAsObject(&ResultAsset, "SampleAsset", nil, 0, 0)278	for i := 1; i <= 100; i++ {279		var (280			model     ModelAssetMaintenance281			startrand time.Time282			hourVal   time.Duration283		)284		model.Assets = ResultAsset[rand.Intn(len(ResultAsset))]285		dateRand = strconv.Itoa(ManufacturedYearVal[rand.Intn(len(ManufacturedYearVal))]) + "/" + Monthstring[rand.Intn(len(Monthstring))] + "/" + strconv.Itoa(CapacityVal[rand.Intn(len(CapacityVal))]) + " 00:00:00"286		// model.StartsTime, _ = fmtdate.Parse("YYYY/MM/DD hh:mm:ss", dateRand)287		startrand, _ = fmtdate.Parse("YYYY/MM/DD hh:mm:ss", dateRand)288		// dateRand = strconv.Itoa(ManufacturedYearVal[rand.Intn(len(ManufacturedYearVal))]) + "/" + Monthstring[rand.Intn(len(Monthstring))] + "/" + strconv.Itoa(CapacityVal[rand.Intn(len(CapacityVal))]+3) + " 00:00:00"289		hourVal = hourRand[rand.Intn(len(hourRand))]290		model.StartsTime = startrand.Add(hourVal * time.Hour)291		hourVal = hourRand[rand.Intn(len(hourRand))]292		model.StopTime = model.StartsTime.Add(hourVal * time.Hour)293		model.Duration = model.StopTime.Hour() - model.StartsTime.Hour()294		model.MaintenanceStatus = StatusVal[rand.Intn(len(StatusVal))]295		model.MaintenanceBy = "eaciit"296		model.MaintenanceGroup = GroupVal[rand.Intn(len(GroupVal))]297		model.ChangedParts = "Parts " + strconv.Itoa(i)298		model.CausedBy = "Broken"299		model.ConditionBefore = "Good"300		model.ConditionAfter = "Damaged"301		model.ConditionPercentage = PersenVal[rand.Intn(len(PersenVal))]302		model.Notes = "Note " + strconv.Itoa(i)303		Save("SampleAssetMaintenance", model)304	}305}306type ModelAssetFinancial struct {307	Assets            ModelAsset308	Period            time.Time309	Revenues          float64310	OperationalCost   float64311	MaintenanceCost   float64312	InsuranceCost     float64313	OtherCost         float64314	SalvageValue      float64315	AcquisitionCost   float64316	SustainingCapital float64317}318func sampleAssetFinancial() {319	var (320		ResultAsset []ModelAsset321		dateRand    string322		Monthstring [6]string323		YearVal     int324	)325	ManufacturedYearVal := [2]int{2015, 2014}326	CapacityVal := [8]int{15, 17, 10, 15, 22, 12, 14, 20}327	RevenuesVal := [8]float64{1500, 1700, 1000, 1500, 2200, 1200, 1400, 2000}328	OptCostVal := [8]float64{1500, 1700, 1000, 1500, 2200, 1200, 1400, 2000}329	MaintCostVal := [8]float64{1500, 1700, 1000, 1500, 2200, 1200, 1400, 2000}330	InsCostVal := [8]float64{1500, 1700, 1000, 1500, 2200, 1200, 1400, 2000}331	OthCostVal := [8]float64{1500, 1700, 1000, 1500, 2200, 1200, 1400, 2000}332	SalvageVal := [8]float64{1500, 1700, 1000, 1500, 2200, 1200, 1400, 2000}333	AcqCostVal := [8]float64{1500, 1700, 1000, 1500, 2200, 1200, 1400, 2000}334	SustCaptVal := [8]float64{1500, 1700, 1000, 1500, 2200, 1200, 1400, 2000}335	PopulateAsObject(&ResultAsset, "SampleAsset", nil, 0, 0)336	for i := 1; i <= 100; i++ {337		var model ModelAssetFinancial338		model.Assets = ResultAsset[rand.Intn(len(ResultAsset))]339		YearVal = ManufacturedYearVal[rand.Intn(len(ManufacturedYearVal))]340		if YearVal == 2014 {341			Monthstring = [6]string{"07", "08", "09", "10", "11", "12"}342		} else {343			Monthstring = [6]string{"01", "02", "03", "04", "05", "06"}344		}345		dateRand = strconv.Itoa(ManufacturedYearVal[rand.Intn(len(ManufacturedYearVal))]) + "/" + Monthstring[rand.Intn(len(Monthstring))] + "/" + strconv.Itoa(CapacityVal[rand.Intn(len(CapacityVal))]) + " 00:00:00"346		model.Period, _ = fmtdate.Parse("YYYY/MM/DD hh:mm:ss", dateRand)347		model.Revenues = RevenuesVal[rand.Intn(len(RevenuesVal))]348		model.OperationalCost = OptCostVal[rand.Intn(len(OptCostVal))]349		model.MaintenanceCost = MaintCostVal[rand.Intn(len(MaintCostVal))]350		model.InsuranceCost = InsCostVal[rand.Intn(len(InsCostVal))]351		model.OtherCost = OthCostVal[rand.Intn(len(OthCostVal))]352		model.SalvageValue = SalvageVal[rand.Intn(len(SalvageVal))]353		model.AcquisitionCost = AcqCostVal[rand.Intn(len(AcqCostVal))]354		model.SustainingCapital = SustCaptVal[rand.Intn(len(SustCaptVal))]355		Save("SampleAssetFinancial", model)356	}357}358type ModelAssetMaintenanceSchedule struct {359	Assets                     ModelAsset360	ScheduledDate              time.Time361	EstimatedMaintenanceDetail float64362	EstimatedCost              float64363}364func sampleAssetMaintenanceSchedule() {365	var (366		ResultAsset []ModelAsset367		dateRand    string368	)369	ManufacturedYearVal := [2]int{2015, 2014}370	Monthstring := [12]string{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}371	CapacityVal := [8]int{15, 17, 10, 15, 22, 12, 14, 20}372	EstimatedDetailVal := [8]float64{1500, 1700, 1000, 1500, 2200, 1200, 1400, 2000}373	EstimatedCostVal := [8]float64{1500, 1700, 1000, 1500, 2200, 1200, 1400, 2000}374	PopulateAsObject(&ResultAsset, "SampleAsset", nil, 0, 0)375	for i := 1; i <= 100; i++ {376		var model ModelAssetMaintenanceSchedule377		model.Assets = ResultAsset[rand.Intn(len(ResultAsset))]378		dateRand = strconv.Itoa(ManufacturedYearVal[rand.Intn(len(ManufacturedYearVal))]) + "/" + Monthstring[rand.Intn(len(Monthstring))] + "/" + strconv.Itoa(CapacityVal[rand.Intn(len(CapacityVal))]) + " 00:00:00"379		model.ScheduledDate, _ = fmtdate.Parse("YYYY/MM/DD hh:mm:ss", dateRand)380		model.EstimatedMaintenanceDetail = EstimatedDetailVal[rand.Intn(len(EstimatedDetailVal))]381		model.EstimatedCost = EstimatedCostVal[rand.Intn(len(EstimatedCostVal))]382		Save("SampleAssetMaintenanceSchedule", model)383	}384}...

Full Screen

Full Screen

Dao.go

Source:Dao.go Github

copy

Full Screen

...10	"strings"11)12// 统一模型接口13type Model interface {14	ToString() string15}16// 读取表(文件)数据17func RfData(table string, key string, data map[string]Model) error {18	// 读取"表"文件,包含文件路径及文件名19	fileName := path + table + subfix20	f, ferr := os.Open(fileName)21	if ferr != nil {22		panic("读取文件失败。file:" + fileName)23	}24	// 延迟执行:关闭文件资源流25	defer f.Close()26	// 创建读取的文件的缓冲区27	buf := bufio.NewReader(f)28	/* 遍历文件数据29	分隔符30		列:每一行以英文逗号分隔31		字段:第一行为字段32		数据:非第一行,以换行符为分隔33	*/34	field := make([]string, 0)35	rowNum := 036	for {37		row, rerr := buf.ReadBytes('\n')38		rowNum++39		fmt.Println("读取第几行:", rowNum)40		if rerr != nil {41			if rerr == io.EOF { // 结束行。原文件最后一行数据数据之后应换行,否则读取不到最后一行的数据42				fmt.Println("读取文件结束符")43				break44			}45			panic("读取文件-行失败。file:" + fileName)46		}47		rowData := strings.Split(strings.TrimSuffix(string(row), "\n"), ",")48		if len(field) == 0 { // 第一行存储字段49			field = rowData50			fmt.Println("读取到的字段为:", field)51		} else { // 非第一行存储数据52			fmt.Println("读取非字段行的行数据为:", rowData)53			merr := toModel(table, key, data, rowData, field)54			return merr55		}56	}57	return nil58}59/* 存储数据到 models601. 根据 table 得到模型612. 利用反射对模型进行赋值623. 再把模型存储到data中63*/64func toModel(table string, key string, data map[string]Model, rowData []string, field []string) error {65	// 1. 根据 table 得到模型66	if _, ok := sysModels[table]; !ok {67		return errors.New("需要存储的模型不存在")68	}69	fmt.Printf("得到的模型。原数据:%v, 类型:%T \n", sysModels[table], sysModels[table])70	71	// 2. 利用反射对模型进行赋值72	// 获取模型的结构体73	arg := []reflect.Value{}74	modelValue := reflect.ValueOf(sysModels[table])75	modelStructFunc := modelValue.Call(arg) // Call方法使用输入的参数in调用v持有的函数:func (v Value) Call(in []Value) []Value76	fmt.Printf("Reflect Call 后的类型:%T ;原值:%v, \n", modelStructFunc, modelStructFunc)77	modelFunc := modelStructFunc[0]78	fmt.Printf("Reflect Call 取[0]的类型:%T ;原值:%v, \n", modelFunc, modelFunc)79	80	var keyValue string81	for i, colDat := range rowData {82		// 查询字段的值83		if field[i] == key {84			keyValue = colDat85		}86		// 获取模型中对应设置相应字段的方法87		methodName := "Set" + strings.Title(field[i])88		fmt.Println("拼接成的方法名:"+methodName)89		90		fieldSetFunc := modelFunc.MethodByName(methodName)91		fieldSetFunc.Call([]reflect.Value{92			reflect.ValueOf(toTypeValue(modelFunc, field[i], colDat)),93		})94		95		/*getMethodName := "Get" + strings.Title(field[i])96		fieldGetFunc := modelFunc.MethodByName(getMethodName)97		getValue := fieldGetFunc.Call([]reflect.Value{})98		fmt.Printf("%s 之后 %s 的 类型:%T;值:%v \n", methodName, getMethodName, getValue[0], getValue[0])*/99	}100	fmt.Printf("查询的主键:%v;主键值:%v \n", key, keyValue)101	fmt.Printf("modelFunc.Interface() 类型:%T;值:%v \n", modelFunc.Interface(), modelFunc.Interface())102	103	data[keyValue] = modelFunc.Interface().(Model)104	return nil105}106func toTypeValue(modelV reflect.Value, field, value string) interface{} {107	mtype := modelV.Elem().FieldByName(field).Type().Name() // [<推荐] // 另外一种写法:mtype := reflect.Zero(modelV.Type().Elem()).FieldByName(field).Type().Name()108	switch mtype {109	case "int":110		b, _ := strconv.Atoi(value)111		return b112	}113	return string(value)114}115// 将数据写入文件116func fwrite(table string, userData map[string]Model) bool {117	// 获取到转化的数据内容118	content := getModelsToString(userData)119	// 打开文件120	// 不管是 Unix 还是 Windows,都需要使用 0666121	filePath := path+table+subfix122	outfile, outErr := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)123	if outErr != nil {124		fmt.Println("文件找不到. file:", filePath)125		return false126	}127	defer outfile.Close()128	// 创建写入的缓冲区129	outbufwri := bufio.NewWriter(outfile)130	// 写入内容131	outbufwri.WriteString(content + "\n")132	// 刷新缓冲区保存内容133	outbufwri.Flush()134	135	return true136}137// 把模型数据源转化为字符串138func getModelsToString(models map[string]Model) string {139	// 记录字段内容140	var fields string141	// 循环处理数据142	var content string143	for _, model := range models {144		if fields == "" {145			// 利用反射获取字段内容146			rmodel := reflect.ValueOf(model)147			modelZ := rmodel.Elem() // [<推荐] // 另外一种写法 modelZ := reflect.Zero(rmodel.Type().Elem())148			// fmt.Printf("modelZ 类型:%T 值:%s", modelZ, modelZ)149			for i := 0; i < modelZ.NumField(); i++ {150				fields = fields + modelZ.Type().Field(i).Name + ","151			}152			fields = strings.TrimSuffix(fields, ",")153		}154		// 记录数据内容155		content = content + model.ToString() + "\n"156	}157	// 最终内容158	return fields + "\n" + strings.TrimSuffix(content, "\n")159}...

Full Screen

Full Screen

generator.go

Source:generator.go Github

copy

Full Screen

...130func toLowerCamelCase(input string) string {131	return (strings.ToLower(input[:1]) + input[1:])132}133func addUnderscore(input string) string {134	return strings.Replace(strings.ToLower(underscoreRegexp.ReplaceAllString(input, `${1}_${2}`)), ".", "_", -1)135}136func stripVersion(input string) string {137	return versionRegex.ReplaceAllString(input, "")138}139func capitalize(s string) string {140	if len(s) <= 1 {141		return strings.ToUpper(s)142	}143	return strings.ToUpper(s[:1]) + s[1:]144}...

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import "fmt"2type Person struct {3}4func (p *Person) String() string {5    return fmt.Sprintf("Name: %s, Age: %d", p.Name, p.Age)6}7func main() {8    p := Person{"John", 30}9    fmt.Println(p)10}11import "fmt"12type Person struct {13}14func (p *Person) String() string {15    return fmt.Sprintf("Name: %s, Age: %d", p.Name, p.Age)16}17func main() {18    p := Person{"John", 30}19    fmt.Println(&p)20}21import "fmt"22type Person struct {23}24func (p Person) String() string {25    return fmt.Sprintf("Name: %s, Age: %d", p.Name, p.Age)26}27func main() {28    p := Person{"John", 30}29    fmt.Println(p)30}31import "fmt"32type Person struct {33}34func (p Person) String() string {35    return fmt.Sprintf("Name: %s, Age: %d", p.Name, p.Age)36}37func main() {38    p := Person{"John", 30}39    fmt.Println(&p)40}

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    p := model.Person{Name: "John", Age: 20}4    fmt.Println(p.String())5}6import "fmt"7type Person struct {8}9func (p Person) String() string {10    return fmt.Sprintf("%v (%v years)", p.Name, p.Age)11}12John (20 years)13import (14func main() {15    p := model.Person{Name: "John", Age: 20}16    fmt.Println(p)17}18import "fmt"19type Person struct {20}21func (p Person) String() string {22    return fmt.Sprintf("%v (%v years)", p.Name, p.Age)23}24John (20 years)25import _ "fmt"26const (27func (i Person) String() string {

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    fmt.Println("Hello, playground")4    m := model.Model{5    }6    fmt.Println(m.String())7}8import "fmt"9type Model struct {10}11func (m Model) String() string {12    return fmt.Sprintf("Name: %v, Age: %v", m.Name, m.Age)13}

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    fmt.Println(model.Person{Name: "Ashish", Age: 23})4}5import (6func main() {7    p := model.Person{Name: "Ashish", Age: 23}8    fmt.Println(p)9}10import (11func main() {12    p := model.Person{Name: "Ashish", Age: 23}13    fmt.Println(p.String())14}15import "fmt"16type Person struct {17}18func (p Person) String() string {19    return fmt.Sprintf("%v (%v years)", p.Name, p.Age)20}21import (22func main() {23    p := model.Person{Name: "Ashish", Age: 23}24    fmt.Printf("%v", p)25}26import (27func main() {28    p := model.Person{Name: "Ashish", Age: 23}29    fmt.Printf("%+v", p)30}31import (32func main() {33    p := model.Person{Name: "Ashish", Age: 23}34    fmt.Printf("%#v", p)35}36import (37func main() {

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    var s1 Student = Student{1, "ram", 20, "A"}4    fmt.Println(s1)5}6import (7func main() {8    var s1 Student = Student{1, "ram", 20, "A"}9    fmt.Println(s1.String())10}11import (12func main() {13    var s1 Student = Student{1, "ram", 20, "A"}14    fmt.Println(s1.String())15}16import (17func main() {18    var s1 Student = Student{1, "ram", 20, "A"}19    fmt.Println(s1.String())20}21import (22func main() {23    var s1 Student = Student{1, "ram", 20, "A"}24    fmt.Println(s1.String())25}26import (27func main() {28    var s1 Student = Student{1, "ram", 20, "A"}29    fmt.Println(s1.String())30}31import (32func main() {33    var s1 Student = Student{1, "ram", 20, "A"}34    fmt.Println(s1.String())35}36import (37func main() {38    var s1 Student = Student{1, "ram", 20, "A"}39    fmt.Println(s1.String())40}41import (42func main() {43    var s1 Student = Student{1, "ram", 20, "A"}

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	obj := model.NewModel()4	obj.SetId(1)5	obj.SetName("Saurabh")6	fmt.Println(obj.GetId())7	fmt.Println(obj.GetName())8	fmt.Println(obj)9}10func (this *Class) GetField() Type {11}12func (this *Class) SetField(value Type) {13}14import (15func main() {16	obj := model.NewModel()17	obj.SetId(1)18	obj.SetName("Saurabh")19	fmt.Println(obj.GetId())20	fmt.Println(obj.GetName())21	fmt.Println(obj)22}

Full Screen

Full Screen

String

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	emp := model.Employee{4	}5	fmt.Println(emp.String())6}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful