未定义错误(类型 []model.Employee 没有字段或方法 EmpNbr)

huangapple go评论95阅读模式
英文:

undefined (type []model.Employee has no field or method EmpNbr) error

问题

以下是我的代码:

第一步:模型文件夹

type EmployeeWithRoot struct {
	Employee []Employee
}

type Employee struct {
	EmpNbr     sql.NullString  `json:"EmpNbr"`
	DateofJoin sql.NullString  `json:"DateofJoin"`
	DeptId     sql.NullString  `json:"DeptId"`
	DeptName   sql.NullString  `json:"DeptName"`
}

第二步:代码文件夹

func GetEmpDetails(logRequestId string, logNestedLevel int, EmpNbr string, DateofJoin string) ([]model.EmployeeWithRoot, error) {

	logFunctionFunctionName := "code.GetEmpDetails"
	logStartTime := time.Now()
	logNestedLevel++

	defer configurations.TimeTrack(logFunctionFunctionName, logRequestId, logStartTime, logNestedLevel)

	rows, err := db.Query(utils.SELECT_OF_EMP_AGGR, EmpNbr, DateofJoin, DeptId, DeptName)

	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var e []model.EmployeeWithRoot

	for rows.Next() {
		var x model.EmployeeWithRoot
		err := rows.Scan(&x.Employee.EmpNbr, &x.Employee.DateofJoin, &x.Employee.DeptId, &x.Employee.DeptName)

		if err != nil {
			return nil, err
		}

		e = append(e, x)
	}

	err = rows.Err()
	if err != nil {
		return nil, err
	}

	return e, nil
}

第三步:API文件夹

Employee, err := code.GetEmpDetails(logRequestId, logNestedLevel, EmpNbr, DateofJoin)
if err != nil {
	log.Panic(err)
}

marshalDataForRequestContentType(logRequestId, logNestedLevel, w, r, Employee)

我是Go语言的新手,遇到了以下错误。请问有人可以帮忙看一下,告诉我缺少代码的哪一行吗?

x.Employee.EmpNbr未定义(类型[]model.Employee没有字段或方法EmpNbr)
x.Employee.DateofJoin未定义(类型[]model.Employee没有字段或方法DateofJoin)
x.Employee.DeptId未定义(类型[]model.Employee没有字段或方法DeptId)
x.Employee.DeptName未定义(类型[]model.Employee没有字段或方法DeptName)

英文:

Below is my code:

Step 1: model folder

type EmployeeWithRoot struct {
	Employee []Employee
}

type Employee struct {
	EmpNbr     sql.NullString  `json:"EmpNbr"`
	DateofJoin      sql.NullString   `json:"DateofJoin"`
        DeptId  sql.NullString  `json:"DeptId"`
        DeptName sql.NullString  `json:"DeptName"`
}

Step 2: code folder

func GetEmpDetails(logRequestId string, logNestedLevel int, EmpNbr string, DateofJoin string) ([]model.EmployeeWithRoot, error) {

	logFunctionFunctionName := "code.GetEmpDetails"
	logStartTime := time.Now()
	logNestedLevel++

	defer configurations.TimeTrack(logFunctionFunctionName, logRequestId, logStartTime, logNestedLevel)

	rows, err := db.Query(utils.SELECT_OF_EMP_AGGR, EmpNbr, DateofJoin, DeptId, DeptName)

	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var e []model.EmployeeWithRoot

	for rows.Next() {
		var x model.EmployeeWithRoot
		err := rows.Scan(&x.Employee.EmpNbr, &x.Employee.DateofJoin, &x.Employee.DeptId,&x.Employee.DeptName)

		if err != nil {
			return nil, err
		}

		e = append(e, x)
	}

	err = rows.Err()
	if err != nil {
		return nil, err
	}

	return e, nil
}

STEP 3: API folder

Employee, err := code.GetEmpDetails(logRequestId, logNestedLevel, EmpNbr, DateofJoin)
	if err != nil {
		log.Panic(err)
	}

	marshalDataForRequestContentType(logRequestId, logNestedLevel, w, r, Employee)

I'm new to go-lang and getting the below error. Can any one please take a look and assist me what line of the code am I missing?

x.Employee.EmpNbr undefined (type []model.Employee has no field or method EmpNbr)
x.Employee.DateofJoin undefined (type []model.Employee has no field or method DateofJoin)
x.Employee.DeptId undefined (type []model.Employee has no field or method DeptId)
x.Employee.DeptName undefined (type []model.Employee has no field or method DeptName)

答案1

得分: 1

你的问题在这里:

var x model.EmployeeWithRoot
err := rows.Scan(&x.Employee.EmpNbr, &x.Employee.DateofJoin, &x.Employee.DeptId,&x.Employee.DeptName)

x.Employee 是一个切片:[]Employee,就像你在模型中定义的那样。

你试图在一个切片上调用 .EmpNbr,显然是行不通的。

这样写才是正确的:

var x model.Employee
err := rows.Scan(&x.EmpNbr, &x.DateofJoin, &x.DeptId,&x.DeptName)
英文:

Your problem is here:

var x model.EmployeeWithRoot
err := rows.Scan(&x.Employee.EmpNbr, &x.Employee.DateofJoin, &x.Employee.DeptId,&x.Employee.DeptName)

x.Employee is a slice: []Employee as you define it in the model.

You are trying to call .EmpNbr on a slice so that obviously doesn't work.

This would work:

var x model.Employee
err := rows.Scan(&x.EmpNbr, &x.DateofJoin, &x.DeptId,&x.DeptName)

huangapple
  • 本文由 发表于 2022年9月27日 10:03:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/73861640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定