我正在学习golang,当我阅读描述Structures的章节时,我遇到了不同的方法来初始化结构.
p1 := passport{}
var p2 passport
p3 := passport{
Photo: make([]byte, 0, 0),
Name: "Scott",
Surname: "Adam",
DateOfBirth: "Some time",
}
fmt.Printf("%s\n%s\n%s\n", p1, p2, p3)
Run Code Online (Sandbox Code Playgroud)
虽然这些打印结构的值为
{ }
{ }
{ Scott Adam Some time}
,下面的代码用&符号打印,因为它是一个引用.
pointerp1 := &p3
fmt.Printf("%s", pointerp1)
pointerp2 := new(passport)
pointerp2.Name = "Anotherscott"
fmt.Printf("%s", pointerp2)
Run Code Online (Sandbox Code Playgroud)
&{ Scott Adam Some time}&{ Anotherscott }
请帮助我解决疑惑.
在用法中pointerp1 := &p3,pointerp1是引用变量to p3,它保存实际数据.同样,保存数据的实际变量是pointerp2什么?
使用这些不同类型的初始化的最佳方案是什么?
这是我在java教程中处理的一个例子.我有一个没有构造函数的Time1类,因此我期望用默认值初始化为int,即零.
public class Time1 {
private int hour; // expected to be initialised with zero
private int minute; // expected to be initialised with zero
private int second; // expected to be initialised with zero
public void setTime(int hour, int minute, int second) {
if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60 || second < 0 || second >= 60) {
throw new IllegalArgumentException("value out of range");
}
this.hour = hour;
this.minute …Run Code Online (Sandbox Code Playgroud) 为愚蠢的问题道歉.我在stackoverflow上检查了相同错误的所有类似问题,但它没有帮助我理解为什么在以下代码中发生此错误.
我有一个额外的头文件和一个源文件,它包含在主文件中,当我编译时,我收到以下错误.我试图char** argv从main()另一个头文件中定义的另一个函数传递.
#include "include/Process.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
if (argc < 2) {
printf("Please provide a path to file\n");
return (EXIT_FAILURE);
}
Process(argv);
Run Code Online (Sandbox Code Playgroud)
Process.h:
#pragma once
extern void Process(char** path);
Run Code Online (Sandbox Code Playgroud)
Process.c:
#include <stdio.h>
#include "../include/Process.h"
#include <stdlib.h>
#include <sys/stat.h>
#include <syslog.h>
#include <sys/types.h>
#include <unistd.h>
void Process(char** path) {
printf("%s\n", path[1]);
}
Run Code Online (Sandbox Code Playgroud)
它被编译但警告是
./src/Process.c:22:6: error: conflicting types for ‘Process’
void Process(char** path) {
^
./include/Process.h:17:6: note: previous declaration of …Run Code Online (Sandbox Code Playgroud) 如果我有一个多行字符串
this is a line
this is another line
Run Code Online (Sandbox Code Playgroud)
删除空行的最佳方法是什么?我可以通过拆分、迭代和进行条件检查来使其工作,但是有更好的方法吗?
在通过在线课程学习java时,我遇到了使用帮助程序类的类型转换.例如:
double d = 5.99;
double do = new Double(d);
int i = do.intvalue();
Run Code Online (Sandbox Code Playgroud)
然而,他们没有解释我如何找到方法,do.intvalue()如果我不知道现有的方法.在Python中,我可以做一个dir(do),它会列出所有方法.如何在Java中检查相同的内容?
我试图通过以下方式创建一片地图.
keyvalue := make(map[string]interface{})
keyvalueslice := make([]keyvalue, 1, 1)
Run Code Online (Sandbox Code Playgroud)
我试图创建它就像创建字符串切片的方式一样,但是我收到一个错误,说keyvalue is not a type.我正在创建此切片以便keyvalueslice稍后将数据附加到变量.
有人可以解释什么是错的吗?
我有一个struct,我有一个new我编写的方法,它生成对象并返回其指针.
现在我还有另一种方法Close,但是到目前为止,创建对象后不必调用此方法.我想确保在创建对象时必须调用此方法.我怎么在Golang做到这一点?如果这是可能的,我不知道这也被称为是什么.请帮忙.谢谢.
我正在使用一个 ldap 对象,我正在从 Activedirectory 检索一些条目。结果是以大写形式返回领域的方式,例如CN=bob,DC=example,DC=com代替cn=bob,dc=example,dc=com。有没有办法有选择地将CN和DC子字符串转换为小写?到目前为止,我strings.split多次使用(首先使用“,”,然后再次使用“=”进行迭代)以达到可以将 CN、DC 等放入列表的程度,然后对它们使用 strings.ToLower。有没有更好更聪明的方法来完成这项工作,可能使用正则表达式,以便我可以避免两次迭代?
在以下代码中,delete[]调用一次以释放 分配的内存new。delete[]但是,调用后数组元素仍然可以访问。我打了delete[]两次电话来确认我收到了double free or corruption错误,我收到了,这意味着内存已被释放。如果内存被释放,我如何访问数组元素?如果我正在将密码之类的内容读入堆中,这可能是一个可能被利用的安全问题吗?
int *foo;
foo = new int[100];
for (int i = 0; i < 100; ++i) {
foo[i] = i+1;
}
cout << foo[90] << endl;
delete[] foo;
cout << foo[90] << endl;
Run Code Online (Sandbox Code Playgroud)
给出以下输出
91
91
和
int *foo;
foo = new int[100];
for (int i = 0; i < 100; ++i) {
foo[i] = i+1;
}
cout << foo[90] << endl;
delete[] foo;
delete[] foo; …Run Code Online (Sandbox Code Playgroud) 我有一个程序,它结合了多个 http 响应并写入文件上的相应搜索位置。我目前正在这样做
client := new(http.Client)
req, _ := http.NewRequest("GET", os.Args[1], nil)
resp, _ := client.Do(req)
defer resp.Close()
reader, _ := ioutil.ReadAll(resp.Body) //Reads the entire response to memory
//Some func that gets the seek value someval
fs.Seek(int64(someval), 0)
fs.Write(reader)
Run Code Online (Sandbox Code Playgroud)
这有时会导致大量内存使用,因为ioutil.ReadAll.
我想bytes.Buffer作为
buf := new(bytes.Buffer)
offset, _ := buf.ReadFrom(resp.Body) //Still reads the entire response to memory.
fs.Write(buf.Bytes())
Run Code Online (Sandbox Code Playgroud)
但还是一样。
我的意图是使用缓冲写入文件,然后再次寻找偏移量,并再次继续写入直到收到流的结尾(从而从 buf.ReadFrom 中捕获偏移值)。但它也将所有内容保存在内存中并立即写入。
将类似的流直接写入磁盘而不将整个内容保留在缓冲区中的最佳方法是什么?
一个理解的例子将不胜感激。
谢谢你。
在main函数中,我有一个gorilla mux路由器,以及一个处理路由的函数.
var router = mux.NewRouter()
func main() {
router.HandleFunc("/", ParseSlash)
http.Handle("/", router)
http.ListenAndServe(":8000", nil)
}
Run Code Online (Sandbox Code Playgroud)
和ParseSlash看起来像
const slash = `<h1>Login</h1>
<form method="post" action="/login">
<label for="name">User name</label>
<input type="text" id="name" name="name">
<label for="password">Password</label>
<input type="password" id="password" name="password">
<button type="submit">Login</button>
</form>`
func ParseSlash(response http.ResponseWriter, request *http.Request) {
fmt.Fprintf(response, slash)
}
Run Code Online (Sandbox Code Playgroud)
但是,在main中,我们并没有将函数称为ParseSlash(),而是ParseSlash在函数内部router.HandleFunc().如果我们没有提供明确的函数,函数从何处获取参数?这种调用函数的方式是什么?
谢谢.
我在做一些条件基础上,iteratating变量够程内检查i,发现它是给我结果我没想到,我决定用一些简单的代码来确认.
for i := 1; i <= 5; i++ {
wg.Add(1)
fmt.Println(i)
go func() {
fmt.Println(i)
wg.Done()
}()
}
wg.Wait()
1
2
3
4
5
6
6
6
6
6
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?有人可以解释为什么6被打印5次,虽然我只迭代到5?
go ×8
methods ×3
arrays ×2
function ×2
http ×2
java ×2
pointers ×2
string ×2
struct ×2
c ×1
c++ ×1
concurrency ×1
dictionary ×1
free ×1
goroutine ×1
hashmap ×1
httpresponse ×1
io ×1
iteration ×1
key-value ×1
loops ×1
object ×1
parameters ×1
python ×1
regex ×1
replace ×1
slice ×1
tolower ×1
tostring ×1