Accesing __ENV. variables from extension

Hi!!

If I run this

k6 run -e MY_HOSTNAME=test.k6.io script.js

I know how to retrieve an env value in the js script:

import http from 'k6/http';
import { sleep } from 'k6';

export default function () {
  const res = http.get(`http://${__ENV.MY_HOSTNAME}/`);
  sleep(1);
}

But how can I retrieve it from the go code in an extension? Did anyone achieve it?

I tried this but it do not work

os.LookupEnv("MY_HOSTNAME") (https://gobyexample.com/environment-variables)

Thanks very much

Have you tried os.Getenv("MY_HOSTNAME")?

1 Like

@0scar

as @rdt.one mentioned, os.Getenv should be what you’re looking for :+1:

On top of that, because naming is hard, I wanted to raise your awareness that in your example what you referred to as a “hostname” was in fact a URL :bowing_man:

Hi,

Finally I understood how it works, and what I need is really os.Getenv(“MY_HOSTNAME”).

Many thanks for the answers!

1 Like