diff --git a/.env b/.env index 0f704df..1acd67d 100644 --- a/.env +++ b/.env @@ -1,10 +1,13 @@ # server -apiport = :8080 +_apiport = :8080 # database user = root password = "12345678" +# qwe + + host = 127.0.0.1 port = 3306 diff --git a/README.md b/README.md new file mode 100644 index 0000000..e54fa53 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# dotenv + +A Go language parsing tool for key value pair configuration files. + +Used in configuration file parsing of back-end small applications. Only the following features are supported, please consider using them. + +### grammar + +This tool parses by line, where one line can be a key value, a comment, or a blank line. + +``` +_id=20 +name = "Kate" +# Comment +``` + +- Keys can be uppercase and lowercase English letters and underscores. +- The equal sign cannot be missing. +- Values are parsed as strings by default, and single and double quotes are automatically ignored. + +### value types + +Structure unmarshalling of the following value types is supported. + +```go +string +int | int8 | int16 | int32 | int64 +uint | uint8 | uint16 | uint32 | uint64 +float32 | float64 +``` + +### install + +``` +go get github.com/bingxio/dotenv +``` + +### example + +See: [parser_test.go](https://github.com/bingxio/dotenv/blob/main/parser_test.go) diff --git a/parser.go b/parser.go index 353f06d..fd6b65f 100644 --- a/parser.go +++ b/parser.go @@ -111,8 +111,9 @@ func unmarshal() (KvSlice, error) { if whiteSpace(); ip >= len(buffer) { return slice, nil } - if markComment(); ip >= len(buffer) { - return slice, nil + if now() == '#' { + markComment() + continue } v, err := markKeyValue() if err != nil { diff --git a/parser_test.go b/parser_test.go index d929ece..0ba60b5 100644 --- a/parser_test.go +++ b/parser_test.go @@ -7,11 +7,11 @@ import ( ) type Config struct { - ApiPort string + ApiPort string `env:"_apiport"` User string Password string Host string - Port int + Port uint Db string `env:"db_name"` }