config

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2024 License: MIT Imports: 10 Imported by: 0

README

CONFIG

Go Reference

Package config provides a simple facility for loading configuration file.

Feature

  • Very simple to use.
  • Easily obtain the execution directory and home directory.

Dependencies

  • go 1.20+

Usage

import "gitee.com/erdian718/config"

// Config represents the configuration information.
type Config struct {
	// ...
}

func main() {
	cfg := Config{
		// The default values...
	}

	if err := config.Load(&cfg, "path", "to", "configuration", "file.json"); err != nil {
		panic(err)
	}

	// ...
}

Note

  • Only support JSON and XML files.
  • Only support UTF-8 and UTF-8 with BOM.

Documentation

Overview

Package config provides a simple facility for loading configuration file.

Example
package main

import (
	"fmt"

	"gitee.com/erdian718/config"
)

// Config represents the configuration information.
type Config struct {
	Default string
	Value   string
}

func main() {
	cfg := Config{
		Default: "default", // The default value.
	}

	if err := config.Load(&cfg, config.ExecDir(), "config.json"); err != nil {
		panic(err)
	}

	fmt.Println(cfg.Default)
	fmt.Println(cfg.Value)
}
Output:

default
value

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecDir

func ExecDir() string

ExecDir returns the directory of the executable that started the current process. This function is goroutine safe.

func HomeDir

func HomeDir() string

HomeDir returns the current user's home directory. This function is goroutine safe.

func Load

func Load(cfg any, elems ...string) error

Load loads configuration from file.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL