Help for the VSCode editor.
-
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
-
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.
-
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 ofmodule_1
- It can be accessed by other packages in
module_1
as long as they importutils
package. - It can be accessed by any application that imports
module_1
.
Reveal
It can be anywhere inside
utils
package, not the rest ofmodule_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
- It can be accessed from anywhere in
-
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.
-
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
toCalcSquare
in both files. Do this, then run it to test...cd /root/code/gofolder go run main.go
-
What information does go.mod file include?
- Go version
- Dependencies needed
- Module replacement information
- All of the above
Reveal
All of the above
-
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 anedit
command, sogo help mod edit
And in the sea of output, you find the
-replace
flag. That's what you need.