我有一个控制器,可以调用三个服务:
public class ProductController() {
@Autowired
private AccountService accountService;
@Autowired
private ProcessService processService;
@Autowired
private releaseService releaseService;
@RequestMapping("/process")
public Product process(@RequestParam(value="name", defaultValue="docs") ProductProcessed process) {
accountService.notify();
releaseService.sendRelease(process);
return processService.process(process);
}
}
Run Code Online (Sandbox Code Playgroud)
}
封装此服务调用的最佳方法是什么?
我正在设计一个简单的应用程序,使用go来读取代表客户文件格式的几种文件格式.我的第一个想法是读取每个文件行,然后将其解析为结构.到目前为止这么好,但我需要根据它的索引拆分每个字段.例如:
line := "1003450020170804890000000022344"
Run Code Online (Sandbox Code Playgroud)
Id从位置1开始到位置4 = 1003 CustomerId是位置5到7以及与该结构相关的所有其他字段.
我想知道是否有更高效的读取格式并应用于此文件行,我想为每个字段创建一些结构并具有开始和结束字段,但这对我来说听起来很奇怪.
type Record struct {
Id int
Date time.Time
Value float64
ClientId int32
}
type RecordId struct {
Start int
Finish int
Value int
}
type ClientId struct {
Start int
Finish int
Value int32
}
Run Code Online (Sandbox Code Playgroud)
我不知道我是否在路上,也许有更优雅的东西在这种情况下会更好.
我有一个下面的函数,我想使其通用:
func genericUnmarshalForType1(file string) Type1 {
raw, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
var type1 Type1
json.Unmarshal(raw, &type1)
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个接受 Type1 或 Type2 的函数,而不需要为每种类型创建一个函数。我怎样才能做到这一点?