Tuesday, April 18, 2023

Not exported by package compiler Unexported Name Error in Go Language

In Go language the functions can be exported with help of the keyword called "package" and same can be reused by importing in other classes with help of  key word called "import".

Even after making the function available to be used in other classes if your receiving the error called "not exported by package " please check if the below solution works for you.

The first letter of the function should be capital letter otherwise even you exported you can't find this function when you try to reference in other classes. So make sure that if any method that you want to re-used please declare with Capital letters.

firstClass.go
=======================================
package connectors

import (
 "fmt",
 "time"
 )
 
 func isNewToGoLang(string mobile) (isnew bool){
 
  if mobile == nil
    return false
  else
    return true
 }
 
 
secondClass.go
==================================
import (
  fclass "/connectors"
)

func checkAccess(){
  resp =: fclass.isNewToGoLang("1234")
  
}

Here I'm trying to access the method called isNewToGoLang() which is delcared in firstClass.go in secondClass.go ,even though i have exported the function still i'm getting an error because the first character of the method is small letter so it will not work.

As mentioned above if i update the method name with capital letter it will work as expected.


firstClass.go
--------------------------------------
package connectors

import (
 "fmt",
 "time"
 )
 
 func IsNewToGoLang(string mobile) (isnew bool){
 
  if mobile == nil
    return false
  else
    return true
 }
 
 
secondClass.go
------------------------------------
import (
  fclass "/connectors"
)

func checkAccess(){
  resp =: fclass.IsNewToGoLang("1234")
  
}

Please comment or write us if you have any queries/requirements.

Please like,follow,bookmark,subscribe this site to receive daily updates.


FaceBook Page - I Love Coding. You?


Hope this helps you..Enjoy..!

No comments:

Post a Comment