我开始学习golang并从JetBrains安装了GoLand.我正在阅读The Go Programming Language一书,在第一章中,作者强烈建议gofmt在每次保存之前使用该工具.我如何在GoLand中遵循这一建议?
在支持可变长度数组之前,我会像这样动态分配它们:
int foo(size_t n)
{
int *arr = malloc(n * sizeof int);
if (!arr) return ENOMEM; /* not enough memory */
.
. else do stuff with arr[]
.
free(arr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用可变长度数组,我现在可以使它看起来更干净:
int bar(size_t n)
{
int arr[n];
.
. do stuff with arr[]
.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但现在我没有"内存不足"检查.事实上,如果n太大,程序会崩溃.
如果n太大,我如何优雅地从bar(n)保释?
我被限制为C(不能使用C++).我希望C有更严格的类型检查.
有没有办法在注释行上获得编译错误?如果有帮助,枚举值不能重叠.
enum hundred {
VALUE_HUNDRED_A = 100,
VALUE_HUNDRED_B
};
enum thousand {
VALUE_THOUSAND_A = 1000,
VALUE_THOUSAND_B
};
void print_hundred(enum hundred foo)
{
switch (foo) {
case VALUE_HUNDRED_A: printf("hundred:a\n"); break;
case VALUE_HUNDRED_B: printf("hundred:b\n"); break;
default: printf("hundred:error(%d)\n", foo); break;
}
}
void print_thousand(enum thousand bar)
{
switch (bar) {
case VALUE_THOUSAND_A: printf("thousand:a\n"); break;
case VALUE_THOUSAND_B: printf("thousand:b\n"); break;
default: printf("thousand:error(%d)\n", bar); break;
}
}
int main(void)
{
print_hundred(VALUE_HUNDRED_A);
print_hundred(VALUE_THOUSAND_A); /* Want a compile error here */
print_thousand(VALUE_THOUSAND_A);
print_thousand(VALUE_HUNDRED_A); /* Want a compile …Run Code Online (Sandbox Code Playgroud) 我想创建一个数据库ABC。我想使用转储文件(例如 ABC_DUMP)将数据加载到 ABC 中。我希望用户 ABC_USER 拥有数据库 ABC 的所有访问权限,包括创建、选择、更改、更新。我使用以下命令登录 psql:
postgres@ubuntu:~$ psql
postgres=# CREATE DATABASE ABC;
postgres=# GRANT ALL PRIVELEGES ON DATABASE ABC TO ABC_USER;
postgres=# \q
postgres@ubuntu:~$ psql -h localhost ABC -U ABC_USER < ABC_DUMP
Password for user ABC_USER xxxxxx
CREATE FUNCTION
ERROR: must be member of role "postgres"
CREATE FUNCTION
ERROR: must be member of role "postgres"
CREATE FUNCTION
ERROR: must be member of role "postgres"
CREATE FUNCTION
ERROR: must be member of role "postgres"
CREATE AGGREGATE
ERROR: must …Run Code Online (Sandbox Code Playgroud)