To fix the “Type has no field or method” error in Go, “correct the field or method name to match the one defined in the type” or “create a method to access the unexported field”.
The “Type has no field or method” error occurs in Go when you access a field or method that doesn’t exist for a specific type.
Here’s an example that demonstrates the error:
package main
import "fmt"
type Student struct {
name string
rollno int
}
func main() {
s := Student{"Rushabh", 30}
fmt.Println("Student's age:", s.age)
}
Output
type Student has no field or method age
In this code, the Student struct has a fields name and rollno, but we are trying to access s.age.
Since there is no field age in the Student struct, the code produces the “type has no field or method” error.
Solution 1
Correct the field or method name to match the one defined in the type.
package main
import "fmt"
type Student struct {
name string
rollno int
}
func main() {
s := Student{"Rushabh", 30}
fmt.Println("Student's name:", s.name)
}
Output
Student's name: Rushabh
Remember that unexported fields (lowercase first letter) are not accessible outside the package they are defined in. In this case, you can export the field (uppercase first letter) or create a method to access the field.
Solution 2
Create a method to access the unexported field:
func (s Student) GetName() string {
return s.name
}
func main() {
s := Student{"Rushabh", 30}
fmt.Println("Student's name:", s.GetName())
}
Output
Student's name: Rushabh
Check the field or method name’s spelling and capitalization, and ensure that the type you are working with has the field or method you are trying to access.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Distributed and cloud computing and is an expert in Go Language.