Computer scienceProgramming languagesGolangPackages and modulesStandard libraryContext package

Context package

Address Book

Report a typo

Complete the program so that when given

  • a string "name" as input it outputs "Sherlock Holmes"
  • a string "age" it outputs 33
  • a string "address" it outputs "Baker Street 221b"

Do not forget to use derived contexts!

Sample Input 1:

name

Sample Output 1:

Sherlock Holmes
Write a program in Go
package main

import (
"fmt"
"context"
)

type ctxKey string

func main() {
var myKey ctxKey
rootCtx := context.Background()
fmt.Scanln(&myKey)
// write your code here
ctxWithName := ...
ctxWithAge := ...
ctxWithAddress := ...
fmt.Println(ctxWithAddress.Value(myKey))
}
___

Create a free account to access the full topic