Cannot import more libraries when using cgo - cgo

I'm attempting to re-write some Go code in C mainly as a learning experience, however I have ran into an issue to which I cannot find the answer to elsewhere.
I am attempting to run the following code:
package conv
/*
#include <stdio.h>
int** ConvertStringToArray(char* str){
printf("%s\n", str);
}
*/
import (
"C"
"unsafe"
)
func ToArrayGo(str string) [][]int {
return nil // TODO
}
func ToArrayC(str string) [][]int {
C.ConvertStringToArray(C.CString(str))
return nil // TODO
}
If I comment out the unsafe import it works just fine, however when I add it in I get the following error with the go install/test commands:
37: error: 'ConvertStringToArray' undeclared (first use in this function)
This is also the case whenever I try to import any other libraries. Any help would be appreciated or even a redirect to a relevant issue (I've already looked but may have missed one).
Thanks,
Dave

import "C"
should be a line unto itself, and the first import. Then you can
import (
"unsafe"
)
as the next line.

Related

how to import a function from another file in swift

I have been asked to do testing in swift and I have no idea about swift
So, I was just reviewing the code and trying to make sense out of it.
I was in a file say xyzTests.swift where we wrote all the test case.
By going through the test cases written in the code, the first thing was something like this
func xyz() {
var failureMessage: String? = nil;
startSess(validity: 3)
}
In Javascript,one can only do something like this if
There is a function declared anywhere in the same file
if we are importing it from somewhere
So I searched in my file to find function declaration from startSess but wasn't able to find it. Then I clicked on jump to definition and found it some other file named utils.swift
internal func startSess(validity: Int = 1) {
}
I checked the file if there was at-least a reference of an import for at-least util but can't find any.
These are all the import statements in my code
import Foundation
#testable import session
Can someone explain me how we can use startSess?
I take it that the startSess is a function that is outside of any class then as it's being called in that way?
what #testable import session does is importing your entire workspace to be reachable from your test class. If you look in the right tab of the test file you can see that the target membership is only selected for tests (as it should be). And therefor it can't reach the rest of your code without the #testable import
See, There are various different methods to call a function from another file Some of them are as given below
(and Pardon me if I left any methods in this answer, I am open to edits :) )
As #Vollan said , importing the whole workspace , so you can access any function from any file anywhere in the project
Calling or Loading the file (in your case util.swift) and using the function in another file(xyz.swift)
ex: In xyz.swift,
func xyz() {
let a = util() //Loading the swiftfile
a.startSess(validity: 3) //using the function of swiftfile
}
making that function global which you want to use.
extension UIViewController { //In most cases UIViewController is used in all files so extending it helps
func startSess(validity: Int = 1) {
}
}
You can directly make that function global even if you are not using any extensions or any classes by using Foundation(As in any file Foundation is must imported in the Header of any SwiftFile)
Ex: - Make any SwiftFile say named abc.swift and in that file
import Foundation
func startSess(validity: Int = 1) {
//Your Code
}
Now, you can call this function by startSess(param) in any file in your whole project.

Error in using Stdin.readLineSync(); in dart

in vs code, i am getting this error in the basic input taking code from the user
my complete code:
import 'dart:io';
void main(){
stdout.write("Enter your name : ");
var name = stdin.readLineSync();
stdout.write(name);
}
Error in the compiler:
playground.dart:9:23: Error: Method not found: 'Stdin.readLineSync'.
String name = Stdin.readLineSync();
^^^^^^^^^^^^
You must add:
import 'dart:io';
before your main function
To learn dart as console application you should use IntelliJ IDEA IDE.
This is the best IDE for dart.
vscode, dartpad does not support stdin stdout.
if you were getting error like
"Getter not found.'stdin'"
in VSCode check for the extension "dart" is installed on your VSCode then checkitout for run i.e dart run command
stdin.readLineSync()!
use the '!' as I did below:
void main(){
stdout.write("Enter your name : ");
var name = stdin.readLineSync()!;
stdout.write(name);
}
The error you get with stdin.readLineSync() is due to null safety introduced in Dart 2.12. Just add (!)
var name = stdin.readLineSync()!;
you should write it -> stdout.writeln and also import the library for it, I amended the code for you below, and it works fine on VSCode
import 'dart:io';
void main(){
stdout.writeln("Enter your name : ");
var name = stdin.readLineSync();
stdout.write(name);
}

No top-level method 'spawnFunction' declared

I'm trying to use Isolates in Dart. The tutorials from dartlang.org seem to use the function spawnFunction. But that does not seem to work for me. And I cant find any docs about this.
import 'dart:isolate';
void doThing() {
print('Hello!');
}
main() {
spawnFunction(doThing);
}
.
Unhandled exception:
No top-level method 'spawnFunction' declared.
The docs from api.dartlang.org mention Isolate.spawn but I get an error saying there is no static method spawn declared.
Did I miss something? A link to appropriate docs (if any) would be appreciated.
Thanks!
Isolate.spawn is indeed the new way of creating isolates. Your example would need to be rewritten as:
import 'dart:isolate';
void doThing(_) {
print("Hello!");
}
main() {
Isolate.spawn(doThing, null);
}
See https://groups.google.com/a/dartlang.org/forum/#!topic/misc/EVUMkZXFXtY for the breaking change announcement.

Duplicate top-level declaration 'METHOD main' in dart

I'm new to dart, and trying to use dart to write a hello world and a unit test, but I get the error:
duplicate top-level declaration 'METHOD main' at ../app.dart::5:6
My project dir is test-dart, and it has 3 files.
test-dart/models.dart
class User {
hello(String name) {
print("Hello, ${name}");
}
}
test-dart/app.dart
#library("app");
#source("./models.dart");
void main() {
new User().hello("app");
}
test-dart/test/test.dart
#library("test");
#import("../app.dart");
void main() {
print("hello, test");
}
Now there is an error in "test.dart" on void main(), the error message is:
duplicate top-level declaration 'METHOD main' at ../app.dart::5:6
The two main() methods are in different libraries, why they are still duplicated? How to fix it?
If you import a library like this #import('../app.dart), then all names from app.dart become visible in the importing code (all public names, actually -- those that don't start with a _). So in your test.dart library, you now have two main functions visible. That is obviously a collision. There are two ways to solve it (that I know of).
First: import the library with a prefix, like this: #import('../app.dart', prefix: 'app'). Then, all public names from app.dart are still visible, but only with an app prefix, so the main function from app.dart is only accessible by app.main. No collision here, but you have to use a prefix everytime.
Second: using a show combinator, like this: #import('../app.dart', show: ['a', 'b']). Then, it is no longer true that all names from app.dart are visible, only those explicitly named (a and b here). I'm not sure if this is already implemented, though.
Maybe in the future, we will get something opposite to the show combinator, so that you could do #import('../app.dart', hide: ['main']). That would be the best solution for your problem, but it isn't in the current language (as specified by 0.09).
You are importing app.dart without a prefix which means that the symbols of the importing and imported library can collide if there are duplicates such as in your example.
To resolve these collisions the library import allows you to prefix imports with an identifier. Your example should work if you change test.dart as follows:
#library("test");
#import("../app.dart", prefix: "app");
void main() {
print("hello, test");
app.main();
new app.User().hello("main");
}
Notice how the classes and top-level functions in the app.dart library are now accessed using the "app" prefix and thus do not collide with the names in test.dart.

LuaBind and package.loadlib

I'm trying to go through the tutorial with luabind here, http://www.rasterbar.com/products/luabind/docs.html, however i'm having trouble loading the library. I'm currently using version 5.1 of lua, so I believe I would use package.loadlib instead of loadlib. I made a simple dll which is this:
#include <iostream>
#include <luabind\luabind.hpp>
void greet()
{
std::cout << "Hello world!\n";
}
extern "C" int init(lua_State* L)
{
luabind::open(L);
luabind::module(L)
[
luabind::def("greet", &greet)
];
return 0;
}
This builds just fine. However I get an error in lua when I try to run this code:
package.loadlib("LuaTestLib.dll", "init")
greet()
It states that greet is nil. How do I load the functions from the dll properly?
From the first two sentences of package.loadlib's documentation:
Dynamically links the host program with the C library libname. Inside this library, looks for a function funcname and returns this function as a C function.
(emphasis added)
This doesn't execute funcname. It simply returns it as a function for you to call. You still have to call it:
package.loadlib("LuaTestLib.dll", "init")()

Resources