In the AWS SDK you will often have to convert a string to a string pointer. For that we can use the “String” function of “github.com/aws/aws-sdk-go-v2/aws”.
mkdir moremodules
cd moremodules
go mod init moremodules
Output:
go: creating new go.mod: module moremodules
Create a main.go:
vi main.go
1 package main
2
3 import (
4 "fmt"
5 "github.com/aws/aws-sdk-go-v2/aws"
6 )
7
8 func main() {
9 aStringPointer := aws.String("Hi")
10 fmt.Println(aStringPointer)
11 fmt.Println(*aStringPointer)
12 }
package main
import (
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
)
func main() {
aStringPointer := aws.String("Hi")
fmt.Println(aStringPointer)
fmt.Println(*aStringPointer)
}
In line 5 we reference and import the github repository “https://github.com/aws/aws-sdk-go-v2".
When we run the the programm:
go run main.go
We get the error
main.go:6:2: no required module provides package github.com/aws/aws-sdk-go-v2/aws; to add it:
go get github.com/aws/aws-sdk-go-v2/aws
To use the module we have to get it. There are several ways to do this:
go get github.com/aws/aws-sdk-go-v2/aws
To just get this single module, or:
go mod tidy
To get all modules:
Output:
go: finding module for package github.com/aws/aws-sdk-go-v2/aws
go: found github.com/aws/aws-sdk-go-v2/aws in github.com/aws/aws-sdk-go-v2 v1.9.0
The referenced modules are also stored in the file go.mod:
cat go.mod
module moremodules
go 1.17
require github.com/aws/aws-sdk-go-v2 v1.9.0
require github.com/aws/smithy-go v1.8.0 // indirect
go run main.go
Output:
0xc000190cb0
Hi
In the first line the adress the string pointer points to is shown and in the second line we tell fmt.Println with the *
to interpret the variable as a string pointer, which shows the string content.