goviper-go

Removal of key-value pair from viper config file


Is there a way to remove a key/value pair from the loaded config file?

viper.Set("key", nil)

does not work


Solution

  • Try

    delete(viper.Get("path.to.key").(map[string]interface{}), "key")
    

    Example:

    [backends]
      [backends.setibe]
        [backends.setibe.servers]
          [backends.setibe.servers.server0]
          url = "http://192.168.1.20:80"
          weight = 1
          [backends.setibe.servers.server1]
          url = "http://192.168.1.21:80"
          weight = 1
    

    To delete "backends.setibe.servers.server1"

    delete(viper.Get("backends.setibe.servers").(map[string]interface{}), "server2")