Skip to content

Latest commit

 

History

History
175 lines (111 loc) · 5.11 KB

01-modules-packages-and-imports.md

File metadata and controls

175 lines (111 loc) · 5.11 KB

Modules, Packages and Imports

Take me to the lab!

Help for the VSCode editor.

  1. For the following module setup in your local environment, choose the correct import statement to import the abc package in module_2
    module_1
    |
    └─── abc
         |
         └─── utils.go
    
    module_2
    
    
    • import "../abc"
    • import <module path for module_1>/abc
    • import "module_1/abc"
    • import module_1/abc/utils
    Reveal

    import <module path for module_1>/abc

    • Go doesn't support relative imports therefore we always need to use a module path
    • The path will have been determined when you did go mod init
  2. Select the valid import statement in Go.
    • import "github/kodekloud/adv-go"
    • import "<https://github.com/kodekloud/adv-go>"
    • import "github.com/kodekloud/adv-go"
    • import "../adv-go"
    Reveal

    import "github.com/kodekloud/adv-go"

    • As before, we can't use relative paths so that rules out one answer.
    • There's no syntax involving < and > so that rules that one out.
    • Module paths look like web URLs (usually to git repos), and github/ on it's own isn't a valid website hostname, so that rules that one out.
  3. Given a variable named points, declared outside of all the functions in a file in a package utils inside the module module_1...

    Select the correct option regarding its accessibility:-

    • It can be accessed from anywhere in module_1.
    • It can be anywhere inside utils package, not the rest of module_1
    • It can be accessed by other packages in module_1 as long as they import utils package.
    • It can be accessed by any application that imports module_1.
    Reveal

    It can be anywhere inside utils package, not the rest of module_1

    • Even though utils is inside (or beneath) module1, it is still a package in its own right.
    • Because points does not start with an uppercase letter, it is not exported outside of the package that declares it, i.e. utils
  4. Imagine a scenario where we need to import mymodule.com/utils (http://mymodule.com/utils) package, but the install location is different...

    Select the correct option to go with in such cases:-

    • Install the package using the correct "go get" command.
    • Changing the import path while installing the package.
    • Using a proxy.
    • Use the replace directive in go.mod file.
    Reveal

    Use the replace directive in go.mod file.

    • For the case where the package path doesn't match the install location (maybe it was moved), we have the replace directive to tell the package loader where to look.
  5. Given these files, make appropriate changes in utils.go for the code in main.go to work.

    main.go file is located at the /root/code/gofolder directory.

    utils.go file is located at the /root/code/gofolder/temp/utils directory.

    Reveal
    • There is actually one change to make in each file
    • Know that for something to be visbile from outside of its own package, the identifier must begin with an uppercase letter.

    Thus the solution is to change calcSquare to CalcSquare in both files. Do this, then run it to test...

    cd /root/code/gofolder
    go run main.go
  6. What information does go.mod file include?
    • Go version
    • Dependencies needed
    • Module replacement information
    • All of the above
    Reveal

    All of the above

  7. Select the correct syntax for replacing the installation location for a package: -
    • go mod edit --require example.com/temp=/Users/priyadav/GolandProjects/examples/temp
    • go mod edit -replace example.com/temp=/Users/priyadav/GolandProjects/examples/temp
    • go mod replace example.com/temp=/Users/priyadav/GolandProjects/examples/temp
    • go mod edit example.com/temp=/Users/priyadav/GolandProjects/examples/temp
    Reveal

    go mod edit -replace example.com/temp=/Users/priyadav/GolandProjects/examples/temp

    You can find the right answer from the terminal...

    go help mod
    

    You notice there's no replace command, but there is an edit command, so

    go help mod edit
    

    And in the sea of output, you find the -replace flag. That's what you need.