V
V
veryoriginalnickname2021-08-15 17:01:03
go
veryoriginalnickname, 2021-08-15 17:01:03

How to initialize a struct within a struct?

There is a structure like this:

type Config struct {
  LogLevel       int
  WriteToConsole bool
  WriteToFile    struct {
    Activated   bool
    Dir      string
    Filename string
    fullPath string
    file *os.File
  }
}

I create:
var loggerConfig = logger.Config{
    LogLevel:       0,
    WriteToConsole: false,
    WriteToFile: struct {
      Activated bool
      Dir       string
      Filename  string
      fullPath  string
      file      *os.File
    }{Activated: true, Dir: "", Filename: ""},
  }

IDE gives error: " Cannot use 'struct { Activated bool Dir string Filename string fullPath string file *os.File }{Activated: true, Dir: "", Filename: ""}' (type struct {...}) as the type struct {...} "
If you leave only:
var loggerConfig = logger.Config{
    LogLevel:       0,
    WriteToConsole: false,
    WriteToFile: {Activated: true, Dir: "", Filename: ""},
  }

Then the error will be: " Missing expression "
How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
veryoriginalnickname, 2021-08-15
@veryoriginalnickname

it was in the non-exported fullPath and file

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question