#Docs example on "Write your first function" does not work on 0.10.2

1 messages · Page 1 of 1 (latest)

finite willow
#

I'm attempting to do this segment of the documentation in golang example, unfortunately the syntax is no longer recognised on new versions of the golang sdk. I tried fixing the code so that I could write a merge request to fix the documentation but I was not able to make it work, here is my code ifx attemp:


import (
    "context"
    "fmt"
    "math"
    "math/rand"
)

type Example struct{}

// Build and publish a project using a Wolfi container
func (m *Example) BuildAndPublish(ctx context.Context, buildSrc *Directory, buildArgs []string) (string, error) {
    var zippedBuildArgs []BuildArg
    for i := 0; i < len(buildArgs); i+=2{
        zippedBuildArgs = append(zippedBuildArgs, BuildArg{Name: buildArgs[i], Value: buildArgs[i]})
    }
    ctr := dag.Wolfi().Container()
    return ctr.
        Build(buildSrc, ContainerBuildOpts{BuildArgs: zippedBuildArgs}).
        Publish(ctx, fmt.Sprintf("ttl.sh/my-hello-container-%.0f", math.Floor(rand.Float64()*10000000))) //#nosec
}```

What is the correct translation of the example using the new syntax?

Write your first function

shadow yew
#

@finite willow is the error on Container.Build?

#

If so, you can do buildSrc.DockerBuild() instead

inner geyser
#

It runs ok @finite willow, have you followed the steps in the previous page Daggerize a project? You need the golang dependency from there. The buildArgs is a list of arguments to pass to go build, it has nothing to do with BuildArg for a docker build.

finite willow
#

Thanks @inner geyser, that was it! I did that example in a clean folder and wasn't aware dagger loads the objects dynamically at install, great to know that this is happening

clever geode
#

@finite willow there is a note on the page which says The steps below assume that you have already [Daggerized the example Go project repository] by initializing a new Dagger Module and installing the Go builder module as a dependency. If not, complete those steps before proceeding. I'm guessing you missed that? Should we make it more prominent?

finite willow
#

@clever geode Oh wow, I did miss that... I think it could maybe help to make this note a disclaimer/warning and say it is a continuation.
A continuation may be more clearer since on this exercise you need the hello folder from the examples.

My mistake becomes quite obvious when I read the explanation just after replacing the code. However, I just got red syntax from my IDE and avoided reading any further until having solved the issue.