0

Hello everyone out there, I've written the following code inside a package

import "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud"

type ConfigNextCloud struct {
    URL      string `json:"url"`
    Username string `json:"username"`
    Password string `json:"password"`
}

func LoadNextCloudProperty(fullFileName string) (ConfigNextCloud, error) { // fullFileName for fetching database credentials from  given JSON filename.
    var configNextCloud ConfigNextCloud
    // Open and read the file
    fileHandle, err := os.Open(fullFileName)
    if err != nil {
        return configNextCloud, err
    }
    defer fileHandle.Close()

    jsonParser := json.NewDecoder(fileHandle)
    jsonParser.Decode(&configNextCloud)

    // Display Information about NextCloud Instance.
    fmt.Println("Read NextCloud configuration from the ", fullFileName, " file")
    fmt.Println("URL\t", configNextCloud.URL)
    fmt.Println("Username \t", configNextCloud.Username)
    fmt.Println("Password \t", configNextCloud.Password)

    return configNextCloud, nil
}

func ConnectToNextCloud(fullFileName string) (*gonextcloud.Client, error) {
    configNextCloud, err := LoadNextCloudProperty(fullFileName)
    //
    if err != nil {
        log.Printf("Loading NextCloudProperty: %s\n", err)
        return nil, err
    }

    fmt.Println("Connecting to NextCloud...")

    nextCloudClient, err := gonextcloud.NewClient(configNextCloud.URL)
    if err != nil {
        fmt.Println("Client creation error:", err)
    }

    if err = nextCloudClient.Login(configNextCloud.Username, configNextCloud.Password); err != nil {
        fmt.Println("Login Error", err)
    }
    defer nextCloudClient.Logout()
    return nextCloudClient, err // return the NextcloudClient created to perform the download and store actions
}

The package containing the aboce code is used in another file. And the complete project has been deployed on github. When I'm trying to run the go get command to install the project I'm getting the following warning:

cannot use nextCloudClient (type gonextcloud.Client) as type *gonextcloud.Client in return argument:
        *gonextcloud.Client is pointer to interface, not interface

Although, the code shows no errors when I'm running the go build command. Please help me with this peculiar issue.

  • 2
    [gonextcloud.Client](https://godoc.org/gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud#Client) is an interface, so return `gonextcloud.Client`, not `*gonextcloud.Client` (a pointer to an interface). – Peter Apr 14 '20 at 12:43
  • 2
    Duplicate: [" is pointer to interface, not interface" confusion](https://stackoverflow.com/questions/44370277/type-is-pointer-to-interface-not-interface-confusion) – Peter Apr 14 '20 at 12:44
  • 1
    Change `ConnectToNextCloud(fullFileName string) (*gonextcloud.Client, error)` to `ConnectToNextCloud(fullFileName string) (gonextcloud.Client, error)` – mkopriva Apr 14 '20 at 12:46
  • @Peter and @mkopriva , I've tried that. But doing that change results in the following errors: ```nextcloud\nextcloud.go:76:3: cannot use nil as type gonextcloud.Client in return argument nextcloud\nextcloud.go:90:2: cannot use nextCloudClient (type *gonextcloud.Client) as type gonextcloud.Client in return argument``` – Anubhav Aggarwal Apr 14 '20 at 13:12
  • @AnubhavAggarwal if the `Client` is an interface type, as suggested by the original compiler error, then you *can* use `nil` in the return statement. This is because in Go interfaces *are* nilable. If you're truly getting the error you're claiming you're getting then the code you're showing does not seem to be the actual code you're running. – mkopriva Apr 14 '20 at 13:21
  • @AnubhavAggarwal This is unrelated but, do understand that `fmt.Println` does not stop the execution of a function. When you encounter an error it is not enough to just print it to stdout, you should also exit the function because if you don't you're program will be crashing left and right. – mkopriva Apr 14 '20 at 13:26
  • @mkopriva perhaps it is the actual code I'm running. The interesting thing is that if you look at the second error closely in my previous comment then you'll find that the type of returned ```Client``` differs from the one in the original question without making changes to the return statement. – Anubhav Aggarwal Apr 14 '20 at 13:28
  • @AnubhavAggarwal you don't need to make changes to the return statement, you need to make changes to the return type argument in the function's signature. You need to change it from `*gonextcloud.Client` to `gonextcloud.Client`. The `NewClient` function retruns `gonextclout.Client`, which means that that's the type of the `nextCloudClient` variable, but your original function signature defined the return type as `*gonextcloud.Client`, which is why you were getting the error when you tried to return `nextCloudClient`. – mkopriva Apr 14 '20 at 13:32
  • @AnubhavAggarwal of course you could, if you wanted to, keep the pointer return type and change the return statement from `return nextCloudClient, err` to `return &nextCloudClient, err` however in Go pointers to interfaces are almost alway useless and therefore seldom used. So rather than changing the return statement, change the return type in the signature as suggested in my first comment. – mkopriva Apr 14 '20 at 13:34
  • @AnubhavAggarwal take a look at this example: https://play.golang.com/p/ESCfA851Emd maybe it will make it a bit more clear. – mkopriva Apr 14 '20 at 13:40

0 Answers0