Cannot Access Unexported Field

user.name undefined (type User has no field or method name, but does have Name)

Quick Answer

The field or method starts with a lowercase letter making it unexported. Capitalize the first letter to export it, or access it from within the same package.

Why This Happens

In Go, identifiers starting with a lowercase letter are unexported (private to the package). You cannot access them from outside the package. This applies to struct fields, methods, functions, and types. To make them accessible, capitalize the first letter.

The Problem

// package models
type User struct {
    name string // unexported
}

// package main
func main() {
    u := models.User{}
    u.name = "Alice" // cannot access unexported field
}

The Fix

// package models
type User struct {
    Name string // exported
}

// package main
func main() {
    u := models.User{}
    u.Name = "Alice" // works fine
}

Step-by-Step Fix

  1. 1

    Identify the unexported identifier

    Check if the field or method starts with a lowercase letter.

  2. 2

    Decide on the access pattern

    Determine if the field should be exported or if access should go through a getter method.

  3. 3

    Export or add accessor

    Capitalize the first letter to export, or add a public method that returns the private field.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free