将指针转换为字节切片

时间:2017-04-24 14:39:07

标签: pointers go system-calls mmap virtualalloc

Golang中Mmap() package中的x/sys/unix系统调用返回[]byte类型,而底层系统调用实际返回指针。它是如何做到的?

更具体地说,在Golang开发人员的this包中,VirtualAlloc函数只返回一个指针。如何将其转换为字节切片,与在Unix包中完成的方式相同?

2 个答案:

答案 0 :(得分:4)

使用<!DOCTYPE html> <html> <head> <title> login </title> </head> <body> <?php session_start(); // Authenticates existing users ?> <form method="post" action="login.php"> Username: <input type="text" name="username"><br> Password: <input type="password" name="password"><br> <input type="submit" value="Login"> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // Connect to the database $mysqli = new mysqli("localhost", "bwright4", "H01597127", "bwright4"); // Output error info if there was a connection problem if ($mysqli->connect_errno) die("Failed to connect to MySQL: ($mysqli->connect_errno) $mysqli->connect_error"); //Username information $username = mysql_real_escape_string($_POST['username']); // Check username and password $password = $_POST["password"]; $password_hash = password_hash($password, PASSWORD_BCRYPT); // bcrypt hash for "abc" $actual_hash = '$2y$10$yd/WAu0wdbgzPk0TDNVBf.MZ0TD6pwKLM18O73aAwOdYs4291/wha'; $sql = mysql_query("SELECT * FROM users_table WHERE username='$username'"); if(mysql_num_rows($sql) == 1) { $row = mysql_fetch_array($sql); if (password_verify($password, $actual_hash)) { $_SESSION["login"] = "True"; header("Location: index.php"); exit; } else echo "<p>Wrong password</p>"; } } ?> <body> </html> 软件包,unsafe可以在其未导出的golang.org/x/sys/unix类型的Mmap方法中执行相同的操作:

mmapper

这里是playground example

答案 1 :(得分:2)

您可以使用// C data with explicit length to Go []byte func C.GoBytes(unsafe.Pointer, C.int) []byte 之类的内容(例如see here):

{{1}}