👨🏻💻 Вопрос от пользователя
spf13/viper не может прочитать файл
По аналогии с https://github.com/gogjango/gjango/blob/master/config/config.go
вывод
сам файл в папке есть. ось - убунта. права 0775 накинул
Golang задачи с собеседований
spf13/viper не может прочитать файл
По аналогии с https://github.com/gogjango/gjango/blob/master/config/config.go
package config
import (
"fmt"
"log"
"os"
"path/filepath"
"read-clickhouse/tools"
"runtime"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
func check() {
_, filePath, _, _ := runtime.Caller(0)
configName := "config.dev.yaml"
configPath := filePath[:len(filePath)-9] + "files" + string(filepath.Separator)
filename := fmt.Sprintf("%s%s", configPath, configName)
if _, err := os.Stat(filename); err == nil {
fmt.Println("Файл существует")
} else if os.IsNotExist(err) {
fmt.Println("Файл не существует")
} else {
fmt.Println("Ошибка при проверке файла:", err)
}
}
// Load returns Configuration struct
func Load(env string) *Configuration {
check()
_, filePath, _, _ := runtime.Caller(0)
configName := "config." + env + ".yaml"
configPath := filePath[:len(filePath)-9] + "files" + string(filepath.Separator)
tools.DebugPrintf(configName)
viper.SetConfigName(configName)
viper.AddConfigPath(configPath)
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}
var config Configuration
viper.Unmarshal(&config)
setGinMode(config.Server.Mode)
return &config
}
// Configuration holds data necessery for configuring application
type Configuration struct {
Server *Server `yaml:"server"`
}
// Server holds data necessary for server configuration
type Server struct {
Mode string `yaml:"mode"`
}
func setGinMode(mode string) {
switch mode {
case "release":
gin.SetMode(gin.ReleaseMode)
break
case "test":
gin.SetMode(gin.TestMode)
break
default:
gin.SetMode(gin.DebugMode)
}
}
вывод
go run main.go
Файл существует
[/home/des/proj/go-read-clickhouse/config/config.go:read-clickhouse/config.Load 38]
%!(EXTRA string=config.dev.yaml)
2024/02/02 16:34:51 Config File "config.dev.yaml" Not Found in "[/home/des/proj/go-read-clickhouse/config/files]"
exit status 1
make: *** [Makefile:2: run] Ошибка 1
сам файл в папке есть. ось - убунта. права 0775 накинул
Golang задачи с собеседований
What does the following program print?
Anonymous Quiz
11%
[0 1 2 3] [0 2 3 3 3]
19%
[0 2 3 3] [0 2 3 3 3]
49%
[0 1 2 3] [0 2 3 2 3]
22%
[0 2 3 3] [0 2 3 2 3]
👎13👍5
What does the following program print?
Anonymous Quiz
18%
0A,1B,2C,
21%
0A,1Z,2Z,
13%
0A,1M,2C,
4%
0A,1Z,2M,
36%
(infinite loop)
8%
0A,1M,2Z,
🤡7
🤮19👍2🤔1
What does the following program print?
Anonymous Quiz
22%
012 789
31%
012 987
31%
222 999
16%
222 789
👎12👍2
What does the following program print?
Anonymous Quiz
25%
999 999
57%
555 999
10%
999 555
8%
555 555